abdul qayyum
abdul qayyum

Reputation: 555

TRY CAST is available and TRY CONVERT is not in SQL Server 2012?

I have SQL Server 2012 (v11.0.5058.0 (X64)) on my server. I noticed a weird problem in SSMS. If I write TRY_CAST it says in the tooltip:

TRY_CAST is not recognized as Built-in function name

and intellisense does work for TRY_CONVERT.

When I run my stored procedure:

It executes successfully for TRY_CAST but throws an error for TRY_CONVERT.

EDIT: Following is the exact statements:

DECLARE @var_val varchar(10) = 'test val' --in my case either a float value 
--like 4.2 or characters like **** , \&, ----  
DECLARE @float_val float = TRY_CAST(@var_val as float) -- works but ssms shows error 
--while typing
DECLARE @float_val_con float = TRY_CONVERT(float, @var_val) -- ssms provide intellisence 
--but execution gives error 

Upvotes: 1

Views: 2556

Answers (1)

Praneet Nadkar
Praneet Nadkar

Reputation: 813

Check here, https://learn.microsoft.com/en-us/sql/t-sql/functions/try-cast-transact-sql?view=sql-server-2017 It says TRY_CAST is not a new reserved keyword and is available in all compatibility levels

Which means, TRY_CAST will work for all, but TRY_CONVERT (https://learn.microsoft.com/en-us/sql/t-sql/functions/try-convert-transact-sql?view=sql-server-2017) will work only for 110.

ALTER DATABASE database_name SET COMPATIBILITY_LEVEL = 110

This will surely solve this!

Upvotes: 4

Related Questions