August 22, 2006 at 10:29 am
Hi,
I got an error email from my web application this morning. The error was '009282d2' cannot be converted to a column of data type int.
I tested this sample code and it does return 1. IF ISNUMERIC('009282d2') SELECT 'It is a number'
I tested with different values and this is what I found. It returns a 1, if the string starts with one or more numbers, followed by one d or e, followed by one or more numbers. Like this "\d+[d|e]\d+" ? Is there any other instance it would return a 1? And why is it doing this? May be d denotes a decimal and e denotes an exponent? Is there any other way to check whether a string is a valid integer without any alphabets?
Thanks.
August 22, 2006 at 10:31 am
I am sorry, the sample code should be like this
IF ISNUMERIC('009282d2') = 1
SELECT 'It is a number'
August 22, 2006 at 1:06 pm
You may try this out
DECLARE @MyTest VARCHAR(20)
SELECT @MyTest = '009282d1'
SELECT CASE WHEN (ISNUMERIC(@MyTest) = 1 AND CHARINDEX('D',@MyTest) = 0 AND CHARINDEX('E',@MyTest) = 0) THEN 1 ELSE 0 END
Microsoft, Please fix this bug!!!
Ram
August 22, 2006 at 1:13 pm
More discusions available if you search for IsNumeric.
Samples.
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=296033#bm296042
http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=243646&p=1
http://www.sqlservercentral.com/scripts/viewscript.asp?scriptid=897
Regards,
gova
August 22, 2006 at 2:02 pm
Thank you. I will use the ISINTEGER function.
August 22, 2006 at 11:02 pm
What ever are you talking about? There is no ISINTEGER function in MS-SQL Server....
IsNumeric allows anything that can mathematically be converted to a numberic value including scientific notation (e), decimal notation (d), commas, dollar signs, and a world of other things.
Replace your code of...
IF ISNUMERIC('009282d2') = 1
SELECT 'It is a number'
... with ...
IF '009282d2' NOT LIKE '%[^0-9]%'
SELECT 'It is a number'
--Jeff Moden
Change is inevitable... Change for the better is not.
August 23, 2006 at 1:14 am
Perhaps referring to the script on this site for an ISINTEGER UDF?
http://www.sqlservercentral.com/scripts/viewscript.asp?scriptid=897
David
If it ain't broke, don't fix it...
August 23, 2006 at 6:53 am
Yep... that would do it... that was the only 1 of the 3 that I did look at. Thanks, David.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply