When I run the script below I get the following error:
Conversion failed when converting datetime from character string.
Why is this happening? It should only be processing the 'varchar' line, right?
I've tried it with both Simple and Searched Case with the same result. What is the proper way to construct this?
Thanks.
declare @val varbinary(1024), @type varchar(16); set @type = 'varchar'; set @val = cast('hello world' as varbinary(1024)); select case @type when 'varchar' then CAST(@val as varchar(max)) when 'int' then CAST(@val as int) when 'decimal' then CAST(@val as decimal(18,0)) when 'datetime' then CAST(@val as datetime) else @val
end;