Michael Michael - 2 months ago 9
SQL Question

Convert uniqueidentifier type to string

In my database SQL Express 2014 ,

I have table named

Membership
with column named
UserId
.The column type is
uniqueidentifier
.

I want to convert the content of the UserId column to string, for this purpose I use this row:

select convert(nvarchar(50), UserId) as UserId from Membership


But after I execute the row above I still get the set of numbers and letters in a
uniqueidentifier
format.

Updated:

Here the example of the data in the table in column UserId:

3CDDF65A-3F2B-48C9-814B-006AB2B70B5A


I need to cast it to string.

Any idea what I do wrong?How can I convert and display
uniqueidentifier
fomat in string?

Answer

That is the NewID() GUID As unique as it gets in your DB

If you wanted a more readable Id you should have used INT/BigInt as a primary key with Identity(1,1) as your UserID

All of these return exactly the same thing

DECLARE @User_ID UNIQUEIDENTIFIER = NEWID()
SELECT CAST(@User_ID AS NVARCHAR(50))
SELECT CONVERT(NVARCHAR(50),@User_ID)
SELECT @User_ID

unique_identifier is exactly that, a unique identifier, not some kind of encrypted value of a common string or int

Comments