Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 504
Michael Avatar asked Sep 22 '16 08:09

Michael


2 Answers

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

like image 101
Marinus Avatar answered Sep 20 '22 14:09

Marinus


I'm pretty sure that your code SELECT CONVERT(nvarchar(50), NEWID()) AS UserId actually convert uniqueidentifier to string.

If you are looking for another answer, you should specify the result which you expect from the conversion.

like image 33
Denis Reznik Avatar answered Sep 20 '22 14:09

Denis Reznik