How to convert varchar to uniqueidentifier in SQL Server

By FoxLearn 5/16/2024 8:01:30 AM   114
This post shows you How to convert varchar to uniqueidentifier in SQL Server.

Converting VARCHAR to UNIQUEIDENTIFIER in SQL Server involves using the CAST or CONVERT functions. However, since UNIQUEIDENTIFIER is a specific data type that represents a globally unique identifier (GUID), you need to ensure that the values in your VARCHAR are in a format that can be converted to a UNIQUEIDENTIFIER.

Here's an example of how you can do it

id: a79b1ecd95015ae6b9c8aabb07da1020

To convert varchar to uniqueidentifier you can use the sql query as shown below.

SELECT CONVERT(uniqueidentifier,STUFF(STUFF(STUFF(STUFF('a79b1ecd95015ae6b9c8aabb07da1020',9,0,'-'),14,0,'-'),19,0,'-'),24,0,'-'))