Home >Database >Mysql Tutorial >How to Convert Non-Hyphenated VARCHAR UUIDs to UNIQUEIDENTIFIER in SQL Server?
SQL Server: Efficiently Converting Non-Hyphenated VARCHAR UUIDs to UNIQUEIDENTIFIER
Database administrators often encounter the task of converting UUIDs stored as VARCHAR strings to the more efficient UNIQUEIDENTIFIER
datatype. This is particularly crucial when interacting with external systems or optimizing database operations. The challenge arises when these VARCHAR UUIDs lack the standard hyphens.
Here's a solution to handle this conversion:
<code class="language-sql">DECLARE @uuid VARCHAR(50) SET @uuid = 'a89b1acd95016ae6b9c8aabb07da2010' SELECT CAST( SUBSTRING(@uuid, 1, 8) + '-' + SUBSTRING(@uuid, 9, 4) + '-' + SUBSTRING(@uuid, 13, 4) + '-' + SUBSTRING(@uuid, 17, 4) + '-' + SUBSTRING(@uuid, 21, 12) AS UNIQUEIDENTIFIER)</code>
This SQL query directly addresses the problem. It extracts the necessary substrings from the non-hyphenated VARCHAR UUID and concatenates them using hyphens to create a correctly formatted UUID string. This formatted string is then cast to the UNIQUEIDENTIFIER
datatype, completing the conversion. This method offers a simple and effective way to ensure compatibility and improve data processing.
The above is the detailed content of How to Convert Non-Hyphenated VARCHAR UUIDs to UNIQUEIDENTIFIER in SQL Server?. For more information, please follow other related articles on the PHP Chinese website!