Categories Tags

Converting INT to UNIQUEIDENTIFIER

Today I needed to convert an integer into a uniqueidentifier column. At first, I thought that SQL Server would just allow me to do it intrinsically (because I can go the reverse way). However, surprisingly this was not the case. I got the following error message:

Msg 529, Level 16, State 2, Line 1

Explicit conversion from data type int to uniqueidentifier is not allowed.

So, I hit up my trusty Google and found that you actually need to do the following:

  • Create a new column (if putting on a table) of type uniqueidentifier
  • UPDATE dbo.Table SET GuidColumn = CONVERT(VARBINARY(16), IntegerColumn)

And then you are done.

Posted in sql-server

Tags: