Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: converting UniqueIdentifier to string in a case statement

People also ask

What data type is Uniqueidentifier SQL Server?

The globally unique identifier (GUID) data type in SQL Server is represented by the uniqueidentifier data type, which stores a 16-byte binary value. A GUID is a binary number, and its main use is as an identifier that must be unique in a network that has many computers at many sites.

What is the use of Newid in SQL?

The NEWID() function in SQL Server creates a unique value of type uniqueidentifier. One use of the NEWID() function is in generating random rows from a table.

Can Uniqueidentifier be null in SQL?

You can make uniqueidentifiers null.

How do you lowercase in SQL?

SQL Server LOWER() FunctionThe LOWER() function converts a string to lower-case.


I think I found the answer:

convert(nvarchar(50), RequestID)

Here's the link where I found this info:

http://msdn.microsoft.com/en-us/library/ms187928.aspx


It is possible to use the convert function here, but 36 characters are enough to hold the unique identifier value:

convert(nvarchar(36), requestID) as requestID

Edit: yes, as noted in the comments, char, or nchar, or any function that can properly manipulate ASCII character tables would do the trick. Then, my excuse is that I usually work in a multilingual/multialphabet environment, and the rule is to go for nvarchar, always. That's my no-brainer way of doing things, sorry. And, if one of these days, some database software starts to generate unique identifier with non-ASCII elements, I will be ready.


In my opinion, uniqueidentifier / GUID is neither a varchar nor an nvarchar but a char(36). Therefore I use:

CAST(xyz AS char(36))

Instead of Str(RequestID), try convert(varchar(38), RequestID)