Convert varchar to uniqueidentifier

Ever wondered how you can convert a varchar without the hyphens or dashes to a uniqueidentifier.

Here is the small t-sql script you can use.

declare @UserId varchar ( 50 )

set @UserId = ’80d002c67a5d47899ead410a7342f03d’

SET @UserId = LEFT(@UserId, 8 )

+ ‘-‘ + SUBSTRING(@UserId, 9, 4)

+ ‘-‘ + SUBSTRING(@UserId, 13, 4)

+ ‘-‘ + SUBSTRING(@UserId, 17, 4)

+ ‘-‘ + SUBSTRING(@UserId, 21, 12)

This will return 80d002c6-7a5d-4789-9ead-410a7342f03d

set @strGuid = ’80d002c67a5d47899ead410a7342f03d’declare @strGUID varchar(50)
set @strGuid = ’80d002c67a5d47899ead410a7342f03d’

Leave a Reply

Fill in your details below or click an icon to log in:

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

%d bloggers like this: