-1

Here is my code. I want only numeric character. If it is other than numeric, should not print.

Here @msg is substring of one of string,which dynamically created.So I can not change type of @msg to int.It can be ',,3' or '4,,,',etc.Sometime @msg contain comma,so I want to check for numeric.Isnumeric() consider comma as numeric.I gives 1 as result.How to handle it?

Declare @msg varchar(5)
    set @msg = ',5'
    if(isnumeric(@msg) = 1)
    begin
        print 'Numeric'
        -- logic
    end
Tony
  • 801
  • 1
  • 7
  • 22
Jui Test
  • 2,399
  • 14
  • 49
  • 76

2 Answers2

0

IsNumeric certainly has idiosyncrasies. They are well discussed in the community. See here for some discussion seesimple talk.

What you actually need to do is not quite clear between the questions and comments, but you might get by with something as simple as checking to make sure there isn't a comma either:

DECLARE @msg VARCHAR(5)
SET @msg = ',5'
IF IsNumeric(@msg) = 1 AND CharIndex(',',@msg,1) = 0
BEGIN
    PRINT 'Numeric'
    -- logic
END

For more ideas on how to do a better IsNumeric see similar questions like efficient-isnumeric-replacements-on-sql-server.

Community
  • 1
  • 1
Karl Kieninger
  • 8,841
  • 2
  • 33
  • 49
0

I think you just want to validate that your variable contains only numeric value or not. use the query..

declare @ClientID varchar(100)
declare @filename varchar(1000)
set @filename = '3081PDShivamCSS_MDaily.Dat'
set @ClientID = substring('3081PDShivamCSS_MDaily.Dat', 1,4)

If @ClientID Not LIKE '%[^0-9]%'
Print Numeric
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52