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
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
uniqueidentifiercolumn 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
-
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;
-
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
245Conversion failed converting a value8114Error converting data type22P02PostgreSQL: invalid input syntax for type
More data conversion and truncation errors
8152String or binary data truncated2628Truncated in table, column241Conversion failed for date and/or time242Out-of-range date conversion8115Arithmetic overflow error converting512Subquery returned more than 1 value8134Divide by zero error encountered220Arithmetic overflow error for data type
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.
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.