// SQL Server error reference

SQL Server error 8169: Conversion to uniqueidentifier failed

A string converted to uniqueidentifier isn't a valid GUID: wrong length, stray characters, or an empty string.

Error number
8169
Severity
16
Category
Data conversion and truncation
Error message
Conversion failed when converting from a character string to uniqueidentifier.

What it means

A uniqueidentifier must be 32 hexadecimal digits, optionally in the 8-4-4-4-12 hyphenated form and optionally in braces. Anything else, including an empty string, fails to convert with 8169.

Common causes

  • Empty strings or placeholder text in a column of GUIDs stored as varchar.
  • Comparing a uniqueidentifier column with a string that isn't a GUID, which forces a conversion of the string.
  • GUIDs with extra characters, such as quotes or whitespace copied along with the value.

How to fix it

  1. Find the values that aren't valid GUIDs.

    SELECT RawId FROM staging.Items WHERE TRY_CONVERT(uniqueidentifier, RawId) IS NULL AND RawId IS NOT NULL;
  2. Convert empty strings to NULL before converting.

    SELECT TRY_CONVERT(uniqueidentifier, NULLIF(LTRIM(RTRIM(RawId)), '')) FROM staging.Items;

Reproduce it safely

SELECT CAST('not-a-guid' AS uniqueidentifier);  -- Msg 8169

Related errors

More data conversion and truncation errors

The code and message text are from the Microsoft Database Engine events and errors reference. Browse the full SQL Server error list or search every database error.

// fewer errors, faster fixes

Work on SQL Server in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQL Server and the other databases you use.