SQL: Query after GUID field

Unlike MySQL, GUID (Globally Unique Identifier) in SQL is a special column data type.

In MySQL you would simply use a char(36) or an binary(16) field that could be easily queried like that:

SELECT * FROM table WHERE char_guid_field = "85eaddd7-2582-480f-b057-e7230da3601b"

/* or */

SELECT * FROM table WHERE binary_guid_field = 0x85eaddd72582480fb057e7230da3601b

/* or */

SELECT * FROM table WHERE binary_guid_field = X'85eaddd72582480fb057e7230da3601b'

Attention: The binary notations “0x” and “X’…’” are case sensitive.

In SQL you have to do an explicit type cast of input strings to uniqueidentifier to achieve the same:

CAST(guid_field as uniqueidentifier) = CAST('85eaddd7-2582-480f-b057-e7230da3601b' as uniqueidentifier)

Further information about GUID’s in SQL:

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.