2014-09-20

Format data types on SQL Server 2012+

I was required to format multiple variables and concat them into a beautiful string. I stumbled upon the FORMAT function. A build-in function since SQL Server 2012.Look at http://msdn.microsoft.com/en-us/library/hh213505(v=sql.110).aspx

Missed the part that states:
The following table lists the acceptable data types for the value argument together with their .NET Framework mapping equivalent types.

They are serious about this. Tried the bit data type and it failed miserably.
So you still have to do a
 convert([n]varchar(1), @bit_value)  
for the value as number or
 case when @bit_value = 1 then 'TRUE' when @bit_value = 0 then 'FALSE' else 'NULL' end  
for the value as text.

No comments: