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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With