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

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.