x

Bug in MSSQL Function: IsNumeric

Has anyone had this problem before? If you use the function IsNumeric in T-SQL it returns the following:

SELECT ISNUMERIC('1d8') -- 1

SELECT ISNUMERIC('1D8') -- 1

SELECT ISNUMERIC('1d') -- 0

SELECT ISNUMERIC('1D') -- 0

I would expect that 1d8 and 1D8 wouldn't be numeric.

I'm using SQL Server 2005.

more ▼

asked Oct 27, 2009 at 06:49 AM in Default

Henk gravatar image

Henk
55 1 1 1

(comments are locked)
10|1200 characters needed characters left

8 answers: sort oldest

Melvyn is right, they are numeric but the odd thing is that you cant convert them to integers. You need to convert them to Float because D denotes to float double precision (inherited from Fortran?).

Try:

SELECT CONVERT(int,'1d8')

If you would like to know if the string is possible to convert to an integer you can use a function I found somewhere a while ago. I can't remember where it came from (maybe from www.aspfaq.com ), and I don't want to take credit for it.

CREATE FUNCTION dbo.isReallyInteger  
(  
    @num VARCHAR(64)  
)  
RETURNS BIT  
 BEGIN  
    IF LEFT(@num, 1) = '-'  
        SET @num = SUBSTRING(@num, 2, LEN(@num))  

    RETURN CASE  
    WHEN PATINDEX('%[^0-9-]%', @num) = 0  
        --AND CHARINDEX('-', @num) <= 1  
        AND @num NOT IN ('.', '-', '+', '^') 
        AND LEN(@num)>0  
        AND @num NOT LIKE '%-%' 
    THEN  
        1  
    ELSE  
        0  
    END  
END  
GO

Try the function with something like this:

DECLARE @num AS VARCHAR(8)='1d8' SELECT  DBO.isReallyInteger(@num)  ,ISNUMERIC(@num),  CASE  WHEN DBO.isReallyInteger(@num) =1 THEN CONVERT(int,@num)  END 
more ▼

answered Oct 27, 2009 at 07:29 AM

Håkan Winther gravatar image

Håkan Winther
15.8k 35 37 48

Watch out for the scalar-function problem though.
Oct 27, 2009 at 08:28 AM Rob Farley
I voted this down because of problems with the implementation. See my reply below.
Oct 27, 2009 at 10:17 AM Peso
(comments are locked)
10|1200 characters needed characters left

ISNUMERIC returns true if the value can be converted to a numeric data type. This includes real, float, money. For that reason non-number characters like '+', '-', 'd', 'e', '$','£' are allowed.

more ▼

answered Oct 27, 2009 at 07:37 AM

Kev Riley gravatar image

Kev Riley ♦♦
54k 47 49 76

(comments are locked)
10|1200 characters needed characters left

Technically, they are numeric, and it's working correctly:

SELECT CONVERT(FLOAT, '1d8') -- 100000000 (1 and 8 zeros) SELECT CONVERT(FLOAT, '1D8') -- 100000000 
more ▼

answered Oct 27, 2009 at 06:57 AM

Melvyn Harbour 1 gravatar image

Melvyn Harbour 1 ♦♦
1.4k 18 20 22

(comments are locked)
10|1200 characters needed characters left

This is one of those scenarios where SQL should have RegEx built-in. 1d8 = 1*10^8, but RegEx would be able to pick it up more nicely.

more ▼

answered Oct 27, 2009 at 07:26 AM

Rob Farley gravatar image

Rob Farley
5.7k 16 18 20

(comments are locked)
10|1200 characters needed characters left

Thank you all for replying.

more ▼

answered Oct 27, 2009 at 07:32 AM

Henk gravatar image

Henk
55 1 1 1

Henk, don't forget to mark the correct answer by clicking on the tick next to it!
Oct 27, 2009 at 07:38 AM Melvyn Harbour 1 ♦♦
I'm new to the site, so thanks for the comment.
Oct 27, 2009 at 08:07 AM Henk
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1951
x58

asked: Oct 27, 2009 at 06:49 AM

Seen: 6054 times

Last Updated: Oct 27, 2009 at 06:53 AM