SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Why doesn’t ISNUMERIC work correctly? (SQL Spackle)


Why doesn’t ISNUMERIC work correctly? (SQL Spackle)

Author
Message
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)SSC Guru (66K reputation)

Group: General Forum Members
Points: 66573 Visits: 18570
Jeff Moden (11/30/2010)
CirquedeSQLeil (11/30/2010)
Good stuff Jeff. Got that spackle article out pretty quick.


Thanks, Jason. Heh... When Steve first came out with the "Requested Articles" forum, I banged out 2 "spackles" that night and 2 more the next night. From the looks of the expected publish dates he's assigned on my contribution page, it looks like he has me scheduled every Wednesday for about 4 weeks. The first one came out on Nov 15th.


I must have missed it. What is the link?



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

mohd.nizamuddin
mohd.nizamuddin
Mr or Mrs. 500
Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)Mr or Mrs. 500 (560 reputation)

Group: General Forum Members
Points: 560 Visits: 198
Thanks Jeff for this nice and very well explained article.
hpoe
hpoe
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 27
autoexcrement (11/30/2010)
sharath.chalamgari (11/30/2010)
Simple and nice article,
some time back i was working on validating a column in the database and i came across this issue and we had this function to validate.


This poster brings up a good point: "-" and ".".


I have used a similar function but I had to make sure that the string could be converted into a float.

So there must be additional checks:
"-" and "+" only at the first position
"." allowed only once

TestCases:

select cast('+1.0'as float) -- OK
select cast('-1.0'as float) -- OK
select cast('1.0-'as float) -- NOK
select cast('1.0+'as float) -- NOK
select cast('.5' as float) -- OK
select cast('5.' as float) -- OK
select cast('5.5.5' as float) -- NOK
DavidBridgeTechnology.com
DavidBridgeTechnology.com
SSC Veteran
SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)SSC Veteran (203 reputation)

Group: General Forum Members
Points: 203 Visits: 234
Very nice article. Well written and now added to my bookmarks.

Thanks for sharing


David Bridge

www.DavidBridgeTechnology.com

David Bridge
David Bridge Technology Limited
www.davidbridgetechnology.com
Brandie Tarvin
Brandie Tarvin
SSC-Dedicated
SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)SSC-Dedicated (37K reputation)

Group: General Forum Members
Points: 37841 Visits: 9273
Even people who think they know should read this article. I actually knew all about the punctuation and currency signs, but then I got to the "e" and "d" part. DOH. Hadn't run into that issue before. Mentioning scientific notation = Good Idea.

Great article. Thumbs up.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
wagner crivelini
wagner crivelini
SSC Veteran
SSC Veteran (262 reputation)SSC Veteran (262 reputation)SSC Veteran (262 reputation)SSC Veteran (262 reputation)SSC Veteran (262 reputation)SSC Veteran (262 reputation)SSC Veteran (262 reputation)SSC Veteran (262 reputation)

Group: General Forum Members
Points: 262 Visits: 282
Good catch... I've never thought about it.
Great article. Short and perfectly clear.
Thank you!
only4mithunc
only4mithunc
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1732 Visits: 2803
Hi Jeff,

Its very nice . Thanks a lot. :-)

Thanks & Regards,
MC
steven.malone
steven.malone
SSC-Enthusiastic
SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)SSC-Enthusiastic (178 reputation)

Group: General Forum Members
Points: 178 Visits: 227
First off thanks, like others I had not stopped to think about "d" and "e" and I am a little surprised "x" was not included if they were. go figure.

However, when I ran your script on my machine the returned data set included 92 \ (backslash).
Is there some setting that affects that?

I expanded your script a little:
--===== Return all characters that ISNUMERIC thinks is numeric
-- (uses values 0-255 from the undocumented spt_Values table
-- instead of a loop from 0-255)
SELECT [Ascii Code] = STR(Number),
[Ascii Character] = CHAR(Number),
[Standalone] = ISNUMERIC(CHAR(Number)),
[CharZero] = ISNUMERIC(CHAR(Number)+'0'),
[ZeroCharZero] = ISNUMERIC('0'+CHAR(Number)+'0'),
[ZeroCharCharZero] = ISNUMERIC('0'+CHAR(Number)+CHAR(Number)+'0'),
[ZeroChar] = ISNUMERIC('0'+CHAR(Number))
FROM Master.dbo.spt_Values
WHERE Type = 'P'
AND Number BETWEEN 0 AND 255
AND (ISNUMERIC(CHAR(Number)) = 1
or 1 = ISNUMERIC(CHAR(Number)+'0')
or 1 = ISNUMERIC('0'+CHAR(Number)+'0')
or 1 = ISNUMERIC('0'+CHAR(Number))
)



And got this result set:
Ascii Code Character Standalone  CharZero    ZeroCharZero ZeroCharCharZero ZeroChar
---------- --------- ----------- ----------- ------------ ---------------- -----------
0 0 0 1 1 1
9 1 1 0 0 0
10 1 1 0 0 0
11 1 0 0 0 1
12 1 0 0 0 1
13 1 1 0 0 0
32 0 1 0 0 1
36 $ 1 1 0 0 0
43 + 1 1 0 0 0
44 , 1 1 1 1 1
45 - 1 1 0 0 0
46 . 1 1 1 0 1
48 0 1 1 1 1 1
49 1 1 1 1 1 1
50 2 1 1 1 1 1
51 3 1 1 1 1 1
52 4 1 1 1 1 1
53 5 1 1 1 1 1
54 6 1 1 1 1 1
55 7 1 1 1 1 1
56 8 1 1 1 1 1
57 9 1 1 1 1 1
68 D 0 0 1 0 0
69 E 0 0 1 0 0
92 \ 1 1 0 0 0
100 d 0 0 1 0 0
101 e 0 0 1 0 0
128 € 1 1 0 0 0
160   1 0 0 0 1
162 ¢ 1 1 0 0 0
163 £ 1 1 0 0 0
164 ¤ 1 1 0 0 0
165 ¥ 1 1 0 0 0


Notice that 44 (comma) has a one in the ZeroCharCharZero column.
Also that 0 has ones in the last three columns.
I tried and
select convert(numeric,'0,,0')
go
select convert(numeric,'0'+char(0)+'0')
go
select convert(numeric,'0'+char(0)+char(0)+'0')
go
select convert(numeric,'0'+char(0))
go

all throw a errors

Bottom line is from now on if I need to validate input I think I will put in a try block assigning the string to a variable of the actual type I need and if I need to validate columns use a type specific function.

Again, thanks for the food for thougt.
svetlana.davkovska
svetlana.davkovska
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 15
This function is OK for integer numbers, what about decimal or negative ?
crashdan
crashdan
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1022 Visits: 757
please also consider 0E0 and numbers like them.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search