I'm using Devart's DotConnect product to connect an .net application to a MySQL Database. Everything is working pretty well, but I have an issue when working with SQL in the database.
The application uses Guids for row id's in the database (having come originally from a SQL Server application) which are stored as BINARY(16) in the database (and use DevArt's MySQLGuid class when being accessed through the .net application)
The problem is that when I want to query the database for a specific row I can't just paste in a string representation of a GUID in the database, so I'm trying to work out a function to convert the binary representation of the GUID to a string.
At first, I though it would be a fairly simple matter of calling HEX(id) in the query and then if I wanted a friendly readable output all I would need to do is add -'s in the appropriate place.
The select statement could look like this (in practice, I'd wrap this as a function):
LOWER(CONCAT(LEFT(HEX(theme_id), 8), '-', MID(HEX(theme_id), 9,4), '-', MID(HEX(theme_id), 13,4), '-', MID(HEX(theme_id), 17,4), '-', RIGHT(HEX(theme_id), 12)))
does not return quite the right GUID. For example, if I store d1dfd973-fa3d-4b90-a1eb-47217162cd40
then the above select statement returns 73d9dfd1-3dfa-904b-a1eb-47217162cd40
It looks like the first 8 bytes have had their order reversed in the group (taking the first part of the string GUID, we have 73d9dfd1
which from a byte order perspective (i.e. treating groups of two characters as one byte) is d1dfd973
, which is the right output.
My question is this - is there an operation that I can do within MySQL's dialect of SQL that will allow me to reverse the byte order of the relevant sections? I could make a more complex LEFT/MID/RIGHT statement but that doesn't feel like a good way of doing it.
An suggestions would be very much appreciated.