foebu
foebu

Reputation: 1415

How to perform a TRY_CAST in SQL Server 2005?

I'm try to perform a TRY_CAST in SQL Server 2005. The query will look like:

SELECT TRY_CAST((select bla bla bla expression) as int)

The expression inside the try_cast might return some strings or something which cannot be converted as INT, that's why I need it (obviously :) ). Unfortunately I wrote my big query for SQL Server 2012, but now I need to use it on a SQL Server 2005.

Is there a workaround?

Thank you!

Upvotes: 1

Views: 1671

Answers (1)

foebu
foebu

Reputation: 1415

In the end I solved with:

case when IsNumeric(Expression)=1
    then cast(Expression as int)
    else NULL
end

Upvotes: 2

Related Questions