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
精彩评论