You’re getting "SQL Server error 235: Cannot convert a char value to money" when trying to perform arithmetic operations on a column defined as CHAR or VARCHAR, not DECIMAL or NUMERIC. This happens because SQL Server expects numeric data types for monetary calculations, not string-based formats. The fix involves ensuring all values in the column are properly converted before math operations.
What You'll Need
- Access to SQL Server Management Studio (SSMS) or a similar tool (query editor, T-SQL editor, or command-line tools like SQLCMD)
- Basic knowledge of T-SQL syntax
- Permissions to modify or query the problematic table
- A backup of your database if you’re altering schema (recommended)
Step-by-Step
-
Identify the column causing the issue:
Run this query to find columns defined as CHAR/VARCHAR that might contain monetary data (even if not numerically formatted):
SELECT column_name, data_type FROM information_schema.columns WHERE table_name LIKE 'YourTableName%' AND data_type IN ('CHAR', 'VARCHAR')Replace 'YourTableName' with the actual table name. Note the column(s) in question. -
Check existing data in the problematic column:
Query all records in the suspect column using a simple SELECT:
SELECT column_name, value FROM YourTableNameLook for values that appear to represent currency (e.g., "$10.50", "£23.99") but aren't numeric strings. These will fail when converted to money. -
Option A: Convert to numeric type (recommended for new columns):
If this is a new column or you can alter the schema safely:
- Create a new numeric column alongside the existing one:
ALTER TABLE YourTableName ADD NewMoneyColumn DECIMAL(18, 2) - Populate the new column by converting string values:
UPDATE YourTableName SET NewMoneyColumn = TRY_CAST(column_name AS DECIMAL(18, 2)) WHERE TRY_CAST(column_name AS DECIMAL(18, 2)) IS NOT NULL;The TRY_CAST ensures only valid conversions proceed. - Drop the original column and rename:
ALTER TABLE YourTableName DROP COLUMN old_column_name; ALTER TABLE YourTableName ADD old_column_name CHAR(100); -- Adjust length ALTER TABLE YourTableName ADD new_column_name DECIMAL(18, 2); EXEC sp_rename 'YourTableName.old_column_name', 'original_name', 'COLUMN'; EXEC sp_rename 'YourTableName.new_column_name', 'converted_name', 'COLUMN';
- Create a new numeric column alongside the existing one:
-
Option B: Fix existing data (if you can't alter schema):
If you must use the original column, create a view with a computed column:
- Create a view that excludes the problematic operation:
CREATE VIEW CleanedView AS SELECT column_name, TRY_CAST(column_name AS DECIMAL(18, 2)) AS numeric_value FROM YourTableName; - Use the view for calculations:
SELECT column_name, numeric_value + 10 AS result FROM CleanedView;
- Create a view that excludes the problematic operation:
-
Option C: Standardize formatting (for reporting):
Use string functions to format currency consistently before display:
SELECT column_name, CASE WHEN TRY_CAST(column_name AS DECIMAL(18, 2)) IS NOT NULL THEN FORMAT(TRY_CAST(column_name AS DECIMAL(18, 2)), '$#.##') ELSE 'N/A' END AS formatted_amount FROM YourTableName;
Common Mistakes
- Using CAST instead of TRY_CAST: CAST will throw an error if conversion fails, while TRY_CAST returns NULL for invalid values, allowing you to handle errors gracefully.
- Ignoring lead/trailing whitespace: Strings like " $10.50 " or "10.50 " need trimming before conversion:
UPDATE YourTableName SET value = LTrim(RTrim(value)) - Assuming all string currency formats work: Currency values can appear as "€12.50", "$20", or "1500" (no decimal). The conversion must handle these variations.
- Altering tables without backups: If you drop columns, ensure you have a full database backup and test the schema change in a staging environment first.
- Overlooking NULL values: String NULLs ('NULL') will fail conversion. Use ISNULL or COALESCE to handle them:
ISNULL(TRY_CAST(column_name AS DECIMAL(18, 2)), 0) - Use dynamic SQL to handle unknown column names:
DECLARE @sql NVARCHAR(MAX); SELECT @sql = 'SELECT column_name, TRY_CAST(column_name AS DECIMAL(18, 2)) AS numeric_value FROM YourTableName;' EXEC sp_executesql @sql; - For large tables, use batch processing in T-SQL:
DECLARE @offset INT = 0, @rowcount INT = (SELECT COUNT(*) FROM YourTableName); WHILE @offset < @rowcount BEGIN UPDATE Top 1000 YourTableName SET NewColumn = TRY_CAST(column_name AS DECIMAL(18, 2)) WHERE @offset + 1 <= (SELECT COUNT(*) FROM YourTableName); SET @offset = @offset + 1000; END; - Add validation to your application logic to prevent invalid currency strings from entering the database.
- Consider using SQL Server's built-in money data type (DECIMAL(18, 2)) for true currency handling, but be aware it has precision limitations (max 2 decimal places).
-
Check for currency symbols in the data:
The error might persist if values contain symbols like $, €, or £. Modify your conversion to strip symbols:
UPDATE YourTableName SET value = REPLACE(REPLACE(REPLACE(value, '$', ''), '€', ''), '£', '') WHERE value LIKE '%$%'; -
Inspect the actual failing operation:
Run the exact query that's failing and trace back to the column:
SELECT * FROM YourTableName WHERE TRY_CAST(column_name AS DECIMAL(18, 2)) + 5;This will reveal if the issue occurs during arithmetic or another operation. -
Check for hidden characters:
Some currency values contain hidden Unicode characters (like zero-width spaces) that fail conversion. Use:
SELECT CONVERT(NVARCHAR(MAX), column_name, 120) AS visible_text FROM YourTableName;to inspect hidden characters. -
Verify the database collation:
If your database uses a collation that treats '£' and '1' as distinct characters (like Latin1_General_CS_AS), conversion may fail. Change collation for affected columns if needed:
ALTER TABLE YourTableName ALTER COLUMN column_name VARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS;(Replace with appropriate collation for your region.) - Validate all currency inputs against a regular expression:
REGEXP_LIKE(value, '^[0-9]+(?:\.[0-9]{1,2})?(?:[ ,]?[0-9]+){0,2}$') - Add a check during INSERT/UPDATE operations:
IF NOT EXISTS (SELECT 1 FROM YourTableName WHERE column_name LIKE '%1,000%' AND value NOT LIKE '%1%000%') - You perform frequent mathematical operations on the column
- The values clearly represent numbers (even if stored as strings)
- You need precise control over decimal places
- The column will be used in calculations across multiple tables
Tips
Troubleshooting
If the above steps don't resolve the issue, check these scenarios:Frequently Asked Questions
What should I do if the column contains thousands separators?
Use the following pattern recognition approach:
UPDATE YourTableName
SET numeric_column = CASE
WHEN value LIKE '%%%' THEN -- Contains multiple spaces (likely thousands separator)
TRY_CAST(SUBSTRING(value, 1, POSITION(' ' IN value) - 1) + '.' +
SUBSTRING(value, POSITION(' ' IN value) + 1, LEN(value)) AS DECIMAL(18, 2))
WHEN value LIKE '%%' THEN -- Contains only one space
TRY_CAST(SUBSTRING(value, 1, POSITION(' ' IN value) - 1) + '.' +
SUBSTRING(value, POSITION(' ' IN value) + 1) AS DECIMAL(18, 2))
ELSE TRY_CAST(value AS DECIMAL(18, 2))
END;
This handles formats like "1,000.50" or "1 000.50".
How can I prevent this error in future applications?
Implement data validation at the application layer:
What if I need to perform currency math across multiple tables?
Use a temporary table for calculations:
-- Create temp table with consistent numeric values
CREATE TABLE #CleanedData AS
SELECT
table1.column1,
TRY_CAST(table1.currency_column AS DECIMAL(18, 2)) AS clean_value
FROM table1;
-- Perform calculations
SELECT
t1.table1_id,
t2.table2_id,
(t1.clean_value + t2.clean_value) AS total_amount
FROM #CleanedData t1
JOIN table2 ON t1.table1_id = table2.table1_id
JOIN #CleanedData t2 ON t2.table1_id = table2.table1_id;
Why does my currency value work in some queries but not others?
The issue likely stems from different collation settings between queries. Check your query execution plans or run:
SELECT SERVERPROPERTY('Collation') AS DatabaseCollation;
Compare with your database collation and ensure consistent handling. Alternatively, wrap the column in a CASE statement:
CASE WHEN SERVERPROPERTY('Collation') = 'SQL_Latin1_General_CP1_CI_AS'
THEN TRY_CAST(column_name AS DECIMAL(18, 2))
ELSE TRY_CAST(REPLACE(column_name, '$', '') AS DECIMAL(18, 2))
How do I handle NULL values in currency columns?
Use COALESCE in your conversion:
UPDATE YourTableName
SET cleaned_value = COALESCE(TRY_CAST(column_name AS DECIMAL(18, 2)), 0)
WHERE column_name IS NOT NULL;
This preserves NULLs and defaults to 0 for invalid conversions. For reporting, use:
ISNULL(TRY_CAST(column_name AS DECIMAL(18, 2)), 0) AS amount
When should I consider changing CHAR/VARCHAR columns to DECIMAL?
Switch to DECIMAL when: