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


ISNUMERIC


ISNUMERIC

Author
Message
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61547 Visits: 13297
Hugo Kornelis (9/27/2010)
da-zero (9/27/2010)

So 1+1 should still be rejected in your opinion.

It is. ISNUMERIC('1+1') returns 0. And that makes sense, because '1+1' does not convert to any of the numerical data types.


Dammit. I knew I should've test it before I made such a statement. :-)
My point was that although + is not a number, the result of the expression 1+1 is a number. So I meant 1+1 as an expression, not '1+1' as a string.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Toreador
Toreador
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3822 Visits: 8123
Hugo Kornelis (9/27/2010)
That all being said, I completely fail to see the acutal use of a function that tells me that a string value "is either a number or its not". What good is it to know that a value is a number if I can still not convert it to a numerical data type without the risk of getting a runtime error? You may see a good use for such a function, but I don't.


The only possible use I can think of is as a preliminary check in a user defined implementation:
if IsNumeric(@value)
begin
--It might be numeric so now do some proper validation
-- (which is probably more resource-expensive than the built-in function
end
else
--Definitely not numeric


But that's just trying to find a use for the sake of it, I don't think I'd bother in real life!
tommyh
tommyh
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2526 Visits: 2000
Hugo Kornelis (9/27/2010)
tommyh (9/27/2010)
I dont agree with your arguments either. A string is either a number or its not. Not depending on what you try and convert it to. It should be one or the other.

If that's your point of view, then you should be happy with the current implementation of ISNUMERIC. "2e1" is a valid number.

I already said that all programming languages I have worked with recognise 1e2 as a floating point representation for 20 (1 x 102). I had at that time not yet verified your assertion that ".NET doesnt do this. Old VB6 does...". I have now. Using Visual Studio 2005, I opened a VB.Net project, edited a line to include the numeric constant "1e3", and I Visual Studio automatically changed it to "1000.0". I then tried the same with "1.23e-15", and VisualStudio changed that to "1.23E-15". Both versions could be compiled and executed.

I then tried the same with a C# project. This time, the code was not changed, but it still compiled and executed without problems.

So, I don't know who made you believe that .NET "doesnt do this" - but if I were you, I'd start double checking all other information from the same source, for it is apparently not the most reliable authority on .NET.


That all being said, I completely fail to see the acutal use of a function that tells me that a string value "is either a number or its not". What good is it to know that a value is a number if I can still not convert it to a numerical data type without the risk of getting a runtime error? You may see a good use for such a function, but I don't.



decimal.TryParse("2e1",out d) gives a False. Int version does the same. So right back at you.

/T
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18641 Visits: 12426
tommyh (9/27/2010)
Hugo Kornelis (9/27/2010)
tommyh (9/27/2010)
I dont agree with your arguments either. A string is either a number or its not. Not depending on what you try and convert it to. It should be one or the other.

If that's your point of view, then you should be happy with the current implementation of ISNUMERIC. "2e1" is a valid number.

I already said that all programming languages I have worked with recognise 1e2 as a floating point representation for 20 (1 x 102). I had at that time not yet verified your assertion that ".NET doesnt do this. Old VB6 does...". I have now. Using Visual Studio 2005, I opened a VB.Net project, edited a line to include the numeric constant "1e3", and I Visual Studio automatically changed it to "1000.0". I then tried the same with "1.23e-15", and VisualStudio changed that to "1.23E-15". Both versions could be compiled and executed.

I then tried the same with a C# project. This time, the code was not changed, but it still compiled and executed without problems.

So, I don't know who made you believe that .NET "doesnt do this" - but if I were you, I'd start double checking all other information from the same source, for it is apparently not the most reliable authority on .NET.


That all being said, I completely fail to see the acutal use of a function that tells me that a string value "is either a number or its not". What good is it to know that a value is a number if I can still not convert it to a numerical data type without the risk of getting a runtime error? You may see a good use for such a function, but I don't.



decimal.TryParse("2e1",out d) gives a False. Int version does the same. So right back at you.

/T

Correct. And double.TryParse("2e1,out d) gives a True and sets d to 20.0. Exactly as expected.
Remember, I was the guy who said that ISNUMERIC() sucks because it doesn't differentiate between data types, and you were the one claiming that a string is either numeric or it isnt. Yet, the .Net code you use to support your point uses different methods for each data type - exactly what I want in SQL Server, and what you say is irrelevant.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
sknox
sknox
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3992 Visits: 2924
foxxo (9/27/2010)
Does the CTE with UNION ALL increment i? It needs to increment in each column? I no comprehende


Yes. This technique is called a recursive CTE. It's a way of generating a sequence of rows without using a tally table. See http://msdn.microsoft.com/en-us/library/ms186243.aspx for more details.
sknox
sknox
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3992 Visits: 2924
Thanks for the good discussion. For the record, I agree with Hugo -- ISNUMERIC doesn't give you very good information because it's test is overly broad.

For example,

\- ,,.,



succeeds in ISNUMERIC() because that converts to a MONEY value of 0.

If you want to see some of the other odd things that are considered numeric by this function, try this:


declare @limit int
set @limit = 4

;with l as (
select 32 as i, char(32) as s, isnumeric(char(32)) as n
union all
select i+1, char(i+1), isnumeric(char(i+1)) from l where i < 127
),
m as (
select cast(l.s as varchar(max)) as ms, isnumeric(l.s) as mn
from l
where ISNUMERIC(l.s) = 1
union all
select ms + cast(l.s as varchar(max)), ISNUMERIC(ms + cast(l.s as varchar(max)))
from m,l
where mn = 1
and datalength(ms) < @limit
and ms not like '%[0-9]%')
select ms from m
where mn = 1 and ms not like '%[0-9]'
option (maxrecursion 0)



This will return all of the strings with length of @limit or below which pass ISNUMERIC() even though they don't contain any digits.
Daniel Bowlin
Daniel Bowlin
SSCertifiable
SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)SSCertifiable (7.9K reputation)

Group: General Forum Members
Points: 7886 Visits: 2629
Good discussion, good question.
Rose Bud
Rose Bud
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1777 Visits: 1062
Experimenting with the code in this question, I am getting different results from two SQL 2008 servers. One evaluates '\' as numeric. The other doesn't. They both have collation SQL_Latin1_General_CP1_CI_AS. Can anyone point me in the right direction on how to research what setting might be different between the two servers? Thanks.
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18641 Visits: 12426
wware (9/27/2010)
Experimenting with the code in this question, I am getting different results from two SQL 2008 servers. One evaluates '\' as numeric. The other doesn't. They both have collation SQL_Latin1_General_CP1_CI_AS. Can anyone point me in the right direction on how to research what setting might be different between the two servers? Thanks.

The first things to look at would be the patch level (SELECT @@VERSION) and the compatibility level (SELECT name, compatibility_level FROM sys.database)


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Rose Bud
Rose Bud
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1777 Visits: 1062
Thank you, Hugo. (The one that evaluates '\' as a number has compatability level 100. The other one has compatability level 80.)
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