Efficient ISNUMERIC() replacements on SQL Server?

后端 未结 11 2077
失恋的感觉
失恋的感觉 2020-11-27 06:02

So I just spent 5 hours troubleshooting a problem which turned out to be due not only to the old unreliable ISNUMERIC but it looks like my problem only appears

11条回答
  •  旧时难觅i
    2020-11-27 06:40

    For SQL Server 2005 and above.... take advantage of try/catch...

    declare @test varchar(10), @num decimal
    select @test = '0123A'
    
    begin try
        select @num = cast(@test as decimal)
        print '1'
    end try 
    begin catch
        print '0'
    end catch
    

    prints 0.

    Change @test = '01234' or @test = '01234.5' and it prints 1.

提交回复
热议问题