Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Why doesn't this CASE statement work?

$
0
0

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;



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>