I figured it out.... Gary was half right... just looking at the wrong place.... and the wrong variable.
It's your case... you're returning up to 5 characters.... so think about it for a sec... what would the RETURN TYPE of the CASE be? The largest possible... a char(5) type....
At first I thought that by wrapping the entire CASE in a convert (or a CAST if you prefer) would work, but that didn't seem to do much good... but if you set each of the return values to a varchar, then it seems to work as you would expect.
compare the results of
to thisCode:declare @num int, @out varchar(10) set @num = 2 set @out = case @num when 1 then char(65) when 2 then char(65) + char(65) when 3 then char(65) + char(65) + char(65) when 4 then char(65) + char(65) + char(65) + char(65) else char(65) + char(65) + char(65) + char(65) + char(65) end) print 'output = |' + @out + '|' print 'output = |' + REPLACE(@out,' ','*') + '|' print 'length of output = ' + cast(len(@out) as varchar)
Code:declare @num int, @out varchar(10) set @num = 2 set @out = case @num when 1 then CONVERT(varchar(5), char(65) ) when 2 then CONVERT(varchar(5), char(65) + char(65)) when 3 then CONVERT(varchar(5), char(65) + char(65) + char(65)) when 4 then CONVERT(varchar(5), char(65) + char(65) + char(65) + char(65)) else CONVERT(varchar(5), char(65) + char(65) + char(65) + char(65) + char(65)) end print 'output = |' + @out + '|' print 'output = |' + REPLACE(@out,' ','*') + '|' print 'length of output = ' + cast(len(@out) as varchar)
-tg




Reply With Quote