Is it posible to CAST or CONVERT with string data type (method that takes data type parametar as string), something like:
CAST('11' AS 'int')
but not
CAST('11' AS int)
No. There are many places in T-SQL where it wants, specifically, a name given to it - not a string, nor a variable containing a name.
If you want to use dynamic sql, this should get you started:
DECLARE @datatype varchar(20)
DECLARE @sql varchar(4000)
SELECT @datatype = 'int'
SELECT @sql = 'PRINT CAST(''11'' AS '+@datatype+')'
exec (@sql)
Depending on what you exactly want/need you should read The Curse and Blessings of Dynamic SQL, especially the parts about sp_executesql
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With