Convert nvarchar to uniqueidentifier in SQL Server

Convert nvarchar to uniqueidentifier in SQL Server

Create function for convert nvarchar to uniqueidentifier as given below.

ALTER FUNCTION [dbo].[Str2Uniqe](@s VARCHAR(50))
RETURNS UNIQUEIDENTIFIER

AS BEGIN
    SET @s = REPLACE(REPLACE(@s,'0x',''),'-','')

    SET @s = STUFF(STUFF(STUFF(STUFF(@s,21,0,'-'),17,0,'-'),13,0,'-'),9,0,'-')
    RETURN CAST(@s AS UNIQUEIDENTIFIER)
END

Select created function result as given below.

DECLARE @guid VARCHAR(50)
SET @guid = '3b8766fa720043af9e21c5bc6bbbdea5'

SELECT dbo.Str2Uniqe(@guid)

Result as given below.


ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book