Home > Database > Mysql Tutorial > How to Convert Non-Hyphenated VARCHAR UUIDs to UNIQUEIDENTIFIER in SQL Server?

How to Convert Non-Hyphenated VARCHAR UUIDs to UNIQUEIDENTIFIER in SQL Server?

Barbara Streisand
Release: 2025-01-24 16:41:10
Original
765 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template