Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New Faq's (IsNumeric, IsInt, IsNumber ) 1

Status
Not open for further replies.

gmmastros

Programmer
Joined
Feb 15, 2005
Messages
14,912
Location
US
I just created a new Faq's. Anyone care to review this for completeness and accuracy?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
faq183-6423

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Could you put some explination in the FAQ about what the '.0e0' and '.e0' do?

Thanks..

Jim
 
George try this

alter Function dbo.IsInteger(@Value VarChar(18))
Returns Bit
As
Begin
Return (Select case IsNumeric(ltrim((@Value)) + '.0e0')when '1' then
case when @Value between '-999999999999999999' and '999999999999999999' then 1 else 0 end else 0 end)
End

then run this

Declare @Temp Table(Data VarChar(18))

Insert Into @Temp Values('$12.09')
Insert Into @Temp Values('1.4e3')
Insert Into @Temp Values('2d4')
Insert Into @Temp Values('3.7')
Insert Into @Temp Values('412')
Insert Into @Temp Values('^')
Insert Into @Temp Values(char(9))

Select ltrim(data),Data,
IsNumeric(Data) As [IsNumeric],
dbo.IsInteger(Data) As IsInteger,
dbo.IsNumber(Data) As IsNumber
From @Temp


And I guess you can do the same for isNumeric

there are more ways to do this like '[0-9]' for example

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Denis

Thanks for the suggestion, I was thinking along these lines...

Code:
Alter Function dbo.IsInteger(@Value VarChar(18))
Returns Bit
As 
Begin
  Return IsNull((Select IsNumeric(@Value + '.0e0') where @Value Like '%[0-9]%'), 0)
End


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I modifed the faq for the IsInteger function and also explained the .0eo and e0 trick. This feedback is very helpful, thanks. Care to take another look?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Cool thanks :)
 
George I don't know if this is nitpicking or not
But the value 412.00 is not returned as being an integer (which techically it is (I think))

Code:
Declare @Temp Table(Data VarChar(18))

Insert Into @Temp Values('$12.09')
Insert Into @Temp Values('1.4e3')
Insert Into @Temp Values('2d4')
Insert Into @Temp Values('3.7')
Insert Into @Temp Values('412.00') --Technically an int  ;-)

Select Data, 
       IsNumeric(Data) As [IsNumeric], 
       dbo.IsInteger(Data) As IsInteger, 
       dbo.IsNumber(Data) As IsNumber
From   @Temp


Denis The SQL Menace
SQL blog:
Personal Blog:
 
ok. It gets messier (I think).

Code:
ALTER  Function dbo.IsInteger(@Value VarChar(18))
Returns Bit
As 
Begin
  
  Return IsNull(
     (Select Case When CharIndex('.', @Value) > 0 
                  Then Case When Convert(int, ParseName(@Value, 1)) <> 0
                            Then 0
                            Else 1
                            End
                  Else 1
                  End
      Where IsNumeric(@Value + 'e0') = 1), 0)


End

Code:
Declare @Temp Table(Data VarChar(18))

Insert Into @Temp Values('$12.09')
Insert Into @Temp Values('1.4e3')
Insert Into @Temp Values('2d4')
Insert Into @Temp Values('3.7')
Insert Into @Temp Values('412.00') --Technically an int  ;-)

Select Data, 
       IsNumeric(Data) As [IsNumeric], 
       dbo.IsInteger(Data) As IsInteger, 
       dbo.IsNumber(Data) As IsNumber
From   @Temp

Denis, I'd like for this to be as bullet proof as possible. Any suggestions are appreciated. Thanks.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
another way

declare @s decimal(18,5)
select @s = 12
select case @s/convert(int,@s) when 1 then 'int' else 'no' end
GO

declare @s decimal(18,5)
select @s = 12.2
select case @s/convert(int,@s) when 1 then 'int' else 'no' end

Denis The SQL Menace
SQL blog:
Personal Blog:
 
Denis,

I thought about converting it to a numeric data type but decided against it because we don't necessarily know what type of number it can convert to, or what the range of the number will. This is why I decided to check isnumeric(value + 'e0') and then use parsename.

Your thoughts?


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top