Using Unicode in Transact SQL / Management Studio

Background

Admittedly, I was trying to be a bit fanciful in showing differences between two columns.

For example, I wanted to artfully show differences when a pair of columns are not the same.

 

select a, b, when a=b then show a=b else a ≠ b

 

Code

Using ≠ Sign

And, so I tried


set nocount on;

--select char(125), char(241), NCHAR(0x2260)

declare @tbl TABLE
(
      [number1] int
    , [number2] int
)

insert into @tbl
([number1], [number2])
values
      (10, 10)
    , (5, 10)

select
          'Using ≠' as 
        , number1
        , number2
        , cast(Number1 as nvarchar)
                 +
                    case
                        when cast(Number1 as int) = cast(Number2 as int)
                            then ' = '
                        else ' ≠ '
                    end
                     + cast(Number2 as nvarchar)
                    as [Result]

from  @tbl

But, got back:
UsingNotEqualSign

 

 

As I do not like 5?10, took to the .Net wondering how to display unicode in Management Studio.  Found a couple of matches:

 

But, unfortunately,  still had the sample ugly character.

 

Using NCHAR (0x2260)

Next tried NCHAR …

set nocount on;

declare @tbl TABLE
(
      [number1] int
    , [number2] int
)

insert into @tbl
([number1], [number2])
values
      (10, 10)
    , (5, 10)

select
          'Using NCHAR(2260) as ≠' as 
        , number1
        , number2
        , cast(Number1 as nvarchar)
                 +
                    case
                        when cast(Number1 as int) = cast(Number2 as int)
                            then ' = '
                        else NCHAR(0x2260)
                    end
                     + cast(Number2 as nvarchar)
                    as [Result]

from  @tbl

 

Works

UsingNchar2260

 

Summary

Please keep in mind that the CHAR function is similar. But, that CHAR only goes as for as ASCII 256.

 

References

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s