I was involved in a discussion today about the ISNUMERIC() function
Someone proposed a nested solution like this
SELECT Column_Name
from (
SELECT Column_Name
FROM (
select 'staff' as Column_Name union all
select '234000' as Column_Name union all
select '12d1' as Column_Name union all
select '45e0' as Column_Name union all
select '$123.45' as Column_Name union all
select '$12,345' as Column_Name
) as Table_Name
WHERE ISNUMERIC(Column_Name) = 1
) as d
where CAST(Column_Name AS INT) <= 1000000
But is does not work. I suggested an alternative method that seems to work.
SELECT d.Column_Name
from (
SELECT x.Column_Name
FROM (
select 'staff' as Column_Name union all
select '234000' union all
select '12d1' union all
select '45e0' union all
select '$123.45' union all
select '$12,345'
) as x
where x.column_name not like '%[^0-9]%'
) as d
where CAST(d.Column_Name AS INT) <= 1000000
However, it does only work on SQL Server 2000!
On SQL Server 2005 I still get a conversion error.
if you comment out the outer second where, and run on SQL Server 2005, you get the expected resultset.
Is this a bug, or feature?
However, I have found a workaround that works in both SQL Server 2000 and SQL Server 2005
SELECT d.ColumnValue
FROM (
SELECT 'staff' AS ColumnValue UNION ALL
SELECT '234000' UNION ALL
SELECT '12d1' UNION ALL
SELECT '45e0' UNION ALL
SELECT '$123.45' UNION ALL
SELECT '$12,345'
) AS d
WHERE d.ColumnValue NOT LIKE '%[^0-9]%'
AND CAST(d.ColumnValue AS INT) <= 1000000
It do seem that SQL Server occasionnaly do short-circuit evaluations. This is one of these times.
Because if you change the order for the two WHERE's, you will get a conversion error.