SQL Server Error 151—*"An invalid money value was found"*—occurs when you try to insert, update, or reference a monetary value that exceeds SQL Server's precision limits (up to 9 decimal places for `money`/`decimal` types) or contains non-numeric characters. This error prevents transactions from completing, often in financial tables, reporting queries, or data migrations. The fix requires identifying where the corrupted value originated, cleaning it up, and ensuring future data validation.

What You'll Need

  • A Microsoft SQL Server instance (Standard, Express, or Developer Edition) with administrative or data-owner permissions.
  • A text editor or SQL Server Management Studio (SSMS) installed on your machine.
  • Backup your database before attempting repairs (critical for production environments).
  • Knowledge of your table structure (column names, types, and constraints).
  • Optional: A tool like DBSQLTool or Redgate SQL Toolbelt for bulk data validation.

Step-by-Step Fixes

  1. Check for recent data entry errors: Run this query to scan all `money`/`decimal` columns for invalid values: SELECT TABLE_SCHEMA + '.' + TABLE_NAME, COLUMN_NAME, DATA_TYPE, CONVERTED_VALUE FROM ( SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, ISNULL(TRY_CAST(CAST(value AS VARCHAR(MAX)) AS DECIMAL(18, 9)), 'Invalid') AS CONVERTED_VALUE FROM ( SELECT DISTINCT s.name AS TABLE_SCHEMA, t.name AS TABLE_NAME, c.name AS COLUMN_NAME, c.max_length, c.precision, c.scale, e.value AS value FROM sys.tables t JOIN sys.schema_objects s ON s.object_id = t.object_id JOIN sys.columns c ON c.object_id = t.object_id AND c.column_id > 0 JOIN sys.extended_properties e ON e.major_id = t.object_id AND e.minor_id = c.column_id WHERE c.user_type_desc IN ('decimal', 'money') AND EXISTS ( SELECT 1 FROM sys.extended_properties e2 WHERE e2.major_id = t.object_id AND e2.minor_id = c.column_id AND e2.value LIKE '%money%' ) ) AS a CROSS APPLY ( SELECT TOP 1 value FROM sys.dm_exec_query_stats q WHERE query_hash = OBJECT_DEFINITION(a.object_id).query_hash ) AS b ) AS c WHERE CONVERTED_VALUE != ISNULL(TRY_CAST(value AS DECIMENT(18, 9)), '') ORDER BY TABLE_NAME, COLUMN_NAME; This query highlights columns with invalid numeric patterns (e.g., text like "ABC" or non-numeric signs).
  2. Validate specific problematic rows: For a table with known issues (e.g., `Orders`), run: SELECT * FROM Orders WHERE column_name IN ('ColumnType', 'Money') AND TRY_CAST(column_value AS DECIMAL(18, 9)) IS NULL; Replace `column_name` with your monetary column and `column_value` with the column alias in your query. Note: Use `TRY_CAST` as a safeguard—it returns `NULL` for invalid values instead of throwing an error.
  3. Fix invalid values in bulk: Use a UPDATE with a conditional check: UPDATE Orders SET column_name = ISNULL(TRY_CAST(column_name AS DECIMAL(18, 9)), 0) FROM Orders WHERE TRY_CAST(column_name AS DECIMAL(18, 9)) IS NULL AND column_name IS NOT NULL; This replaces invalid entries with zeros (customize the replacement value).
  4. Repair corrupted data via SQL Agent: Create a job that runs daily to sanitize data:
    1. Create a temp table: CREATE TABLE #InvalidMoney AS SELECT TABLE_SCHEMA + '.' + TABLE_NAME, COLUMN_NAME, value FROM ( SELECT s.name, t.name, c.name, e.value FROM sys.tables t JOIN sys.schema_objects s ON s.object_id = t.object_id JOIN sys.columns c ON c.object_id = t.object_id AND c.column_id > 0 JOIN sys.extended_properties e ON e.major_id = t.object_id AND e.minor_id = c.column_id WHERE c.user_type_desc IN ('decimal', 'money') AND ISNULL(TRY_CAST(CAST(e.value AS VARCHAR(MAX)), 'VARCHAR(MAX')), '') IS NOT NULL ) AS t;
    2. Export the invalid values to a CSV file: SELECT * FROM #InvalidMoney INTO 'C:\Temp\InvalidMoney.csv'
    3. Manually edit the CSV file to replace invalid entries with valid decimal values.
    4. Re-import the cleaned data using SSIS or a script to replace the original table.

Common Mistakes

  • Skipping backups: Without a backup, you risk permanent data loss during repairs. Always restore from a backup before editing live data.
  • Ignoring precision limits: Values exceeding 9 decimal places (e.g., 123.45678901) or negative numbers without proper formatting (e.g., "-123-ABC") will fail. Use `DECIMAL(18, 9)` for full precision.
  • Using raw string literals: SQL Server treats literal strings (e.g., "'$50'") as text, not numbers. Always cast values explicitly.
  • Assuming `TRY_CAST` fixes everything: The function only handles type conversion. Invalid numeric strings (e.g., "50%") must be replaced manually.
  • Overwriting valid but malformed data: Ensure your UPDATE query only targets rows where validation fails. Validate the result set before applying changes.

Tips

  • For production databases, use a stored procedure to wrap data validation and replacement logic, ensuring consistency.
  • Use SQL Server’s built-in data profiling tools (e.g., DMVs) to identify corrupt data patterns before manual fixes.
  • If you’re importing data from CSV, ensure the source file strictly adheres to `money`/`decimal` formatting rules (e.g., no commas, proper decimal separators).
  • Test fixes in a staging environment first. Recreate the error scenario to verify the solution resolves the issue.
  • Consider adding a trigger to enforce data integrity post-fix: CREATE TRIGGER EnsureMoneyValues ON Orders AFTER INSERT, UPDATE AS SET NOCOUNT ON; DECLARE @Value DECIMAL(18,9); SELECT @Value = CAST(INSERTED.column_name AS DECIMAL(18,9)); IF @Value IS NULL OR @Value < 0 BEGIN RAISERROR('Invalid money value encountered', 16, 1); END; This prevents future errors.

Troubleshooting

If the error persists after basic fixes, try these advanced steps:

  1. Check for truncated values: Run this query to detect columns with potentially truncated numeric data: SELECT TABLE_SCHEMA + '.' + TABLE_NAME, COLUMN_NAME, CAST(value AS VARCHAR(MAX)) AS raw_value, LEN(value) AS length, CASE WHEN CAST(value AS DECIMAL(18, 9)) IS NULL THEN 'INVALID' ELSE 'VALID' END AS validation_result FROM ( SELECT s.name, t.name, c.name, e.value FROM sys.tables t JOIN sys.schema_objects s ON s.object_id = t.object_id JOIN sys.columns c ON c.object_id = t.object_id AND c.column_id > 0 JOIN sys.extended_properties e ON e.major_id = t.object_id AND e.minor_id = c.column_id WHERE c.user_type_desc IN ('decimal', 'money') AND CAST(e.value AS VARCHAR(MAX)) LIKE '%/%' -- Detects truncated decimal patterns ) AS t; Look for entries where the raw text contains percent signs (`%`) or other non-numeric delimiters.
  2. Repair via database restore: If the corruption is limited to one table, extract it from a recent backup, clean the data, and restore the table. Warning: This requires careful schema synchronization and may not work if the table is referenced by other objects (e.g., views, triggers).
  3. Use SQL Server’s repair tools: Check for database corruption with: DBCC CHECKDB Follow up with: DBCC CHECKDB WITH REPAIR_ALLOW_DATA_LOSS (use cautiously in production environments).
  4. Audit the data source: If this error occurs during imports, examine the source file (e.g., Excel, CSV) for:
    • Unescaped commas/special characters.
    • Non-standard decimal separators (e.g., commas instead of periods).
    • Trailing whitespace or line breaks.
    Clean the data in Excel (e.g., "Text to Columns" feature) or a tool like OpenRefine before importing.

Frequently Asked Questions

Why does SQL Server reject my money value?

SQL Server rejects values that exceed the `money`/`decimal` type limits (9 decimal places), contain non-numeric characters, or are negative without proper formatting. For example, "$100.50" is valid, but "100.50%" is invalid.

How do I prevent this error in the future?

Validate all monetary data before importing it into SQL Server. Use a script to sanitize values (e.g., remove currency symbols) or enforce strict data validation in your application layer.

What if I only have a table dump file?

Open the file in a text editor and search for non-numeric patterns (e.g., letters, spaces, or symbols). Replace entries with valid `DECIMAL(18, 9)` values, then import the cleaned file using SSIS or a script.

Can I use a CASE statement to clean invalid values?

Yes, but do it carefully. For example: UPDATE Orders SET column_name = CASE WHEN TRY_CAST(column_name AS DECIMAL(18, 9)) IS NULL THEN 0 WHEN column_name LIKE '%%' THEN 0 -- Replaces percent signs with zeros ELSE TRY_CAST(column_name AS DECIMAL(18, 9)) END; This example handles both invalid casts and truncated decimal patterns.

Why does error 151 happen more in reporting queries?

Reporting queries often fetch data from multiple tables with inconsistent decimal formatting (e.g., one column uses commas as decimal separators while another uses periods). Use `FORMAT()` functions in your reports to standardize output, not to fix data.

Is there a way to restore my database from a backup if the error is critical?

Yes, restore the latest backup to a test environment, manually fix the corrupted table, then replicate the change back to production. Always test this process in a staging environment first.