Problem convert column values from VARCHAR(n) to DECIMAL

Posted by Kevin Babcock on Stack Overflow See other posts from Stack Overflow or by Kevin Babcock
Published on 2010-04-06T23:44:51Z Indexed on 2010/04/06 23:53 UTC
Read the original article Hit count: 253

I have a SQL Server 2000 database with a column of type VARCHAR(255). All the data is either NULL, or numeric data with up to two points of precision (e.g. '11.85'). I tried to run the following T-SQL query but received the error 'Error converting data type varchar to numeric'

SELECT CAST([MyColumn] AS DECIMAL)
FROM [MyTable];

I tried a more specific cast, which also failed.

SELECT CAST([MyColumn] AS DECIMAL(6,2))
FROM [MyTable];

I also tried the following to see if any data is non-numeric, and the only values returned were NULL.

SELECT ISNUMERIC([MyColumn]), [MyColumn]
FROM [MyTable]
WHERE ISNUMERIC([MyColumn]) = 0;

I tried to convert to other data types, such as FLOAT and MONEY, but only MONEY was successful. So I tried the following:

SELECT CAST(CAST([MyColumn] AS MONEY) AS DECIMAL)
FROM [MyTable];

...which worked just fine. Any ideas why the original query failed? Will there be a problem if I first convert to MONEY and then to DECIMAL?

Thanks!

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about sql-server-2000