There are a few problems with the query, but the main problem, the one that is causing the error, is fairly simple: your field_name > 100.00
condition is not converting the VARCHAR
field to a numeric type prior to doing the comparison. Changing it to CONVERT(DECIMAL(38, 18), [field_name])
would fix the error that you are seeing.
Run the following once to see that it does work, then uncomment the AND [field_name] > 100.00
line and run again and you will get the error you have been seeing about "Arithmetic overflow error converting varchar to data type numeric":
DECLARE @table_name TABLE (field_name VARCHAR(50) NOT NULL);
INSERT INTO @table_name (field_name) VALUES ('1234.5678');
INSERT INTO @table_name (field_name) VALUES ('12.78');
INSERT INTO @table_name (field_name) VALUES ('s');
INSERT INTO @table_name (field_name) VALUES ('2015-01-30');
INSERT INTO @table_name (field_name) VALUES ('51234.5678');
INSERT INTO @table_name (field_name) VALUES ('987651234.56789124');
SELECT field_name
FROM @table_name
WHERE ISNUMERIC([field_name]) = 1
AND [field_name] IN (
SELECT CAST([field_name] AS DECIMAL)
FROM @table_name
WHERE ISNUMERIC([field_name]) = 1
AND [field_name] NOT LIKE '%,%'
AND [field_name] NOT LIKE '%-%'
AND [field_name] != '.'
-- AND [field_name] > 100.00
AND CONVERT(DECIMAL(38, 18), [field_name]) > 100.00
);
BUT, there are still some issues:
- The subquery, as shown in the question, makes no sense to return a list of converted values. It is possible that the query has been altered for the purpose of posting here and so makes more sense in its true form, but the subquery isn't needed and isn't helping anyway.
- The
CAST
to DECIMAL
is not specifying the Precision and Scale. The default values are 18 and 0, respectively. Meaning, you are casting the strings into a DECIMAL(18, 0)
. That does not appear to have any impact here, but it is best to specify the values. Running SELECT CAST('123.456' AS DECIMAL)
will return just 123
.
- You don't really need to filter out values with commas as those can be removed, leaving you with a value that can be converted. Unless, of course, you have some that are codes like
12,34,56,78
. But if they are valid numbers, such as 1,234.56
, then you can use REPLACE([field_name], ',', '')
Adding the following two lines to the INSERTs at the top of the example above will error:
INSERT INTO @table_name (field_name) VALUES ('1,234.5678');
INSERT INTO @table_name (field_name) VALUES ('.');
All of this can be handled by doing the following (replace the query above with the one below, but keep the DECLARE
and INSERT
s):
SELECT field_name, CAST([field_name] AS DECIMAL(38, 18)) AS [DecimalValue]
FROM @table_name
WHERE ISNUMERIC(field_name) = 1
AND field_name NOT LIKE '%,%'
AND field_name NOT LIKE '%-%'
AND field_name <> '.'
AND CONVERT(DECIMAL(38, 18), REPLACE([field_name], ',', '')) > 100.00;