开发者

How to silence errors inside a UDF?

开发者 https://www.devze.com 2023-01-25 20:31 出处:网络
I need to write a UDF that would try to cast a varchar to a bigint and re开发者_StackOverflow社区turn either that bigint or zero if failed. The problem is - I can\'t find a way to silence casting erro

I need to write a UDF that would try to cast a varchar to a bigint and re开发者_StackOverflow社区turn either that bigint or zero if failed. The problem is - I can't find a way to silence casting error when the string is not a valid number. TRY CATCH doesnt work inside functions and I don't know what else to do.


Try this:

Create Function dbo.Convert2BigInt(@Data VarChar(100))
Returns BigInt
As 
Begin
    Return(Case When IsNumeric(@Data + '.0e0') = 1 
                Then Convert(BigInt, @Data)
                Else Convert(BigInt, 0)
                End)
End
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号