Wednesday, January 02, 2008

In my opinion SQL Server 2005 is full of holes, and my definition is functionality that is available doesn't span the whole gamete of the server.  For example, aggregates don't work with GUIDs (call uniqueidentifiers in SQL).  Here is non-working code that I would like to see work:

CREATE #Temp(Id uniqueidentifier, Name varchar(50))

SELECT MAX(Id)
FROM #Temp

DROP TABLE #Temp

Why doesn't this work?  GUIDs are just 128-bit numbers, they are all unique and one comes before the other, there should be a MAX and MIN.  This does work:

CREATE #Temp(Id uniqueidentifier, Name varchar(50)) 

SELECT MAX(CONVERT(varchar(41),Id))
FROM #Temp

DROP TABLE #Temp

However, I don't want the extra overhead of converting each GUID to a String.

 

 

GUID | T-SQL | Wayne
Wednesday, January 02, 2008 12:05:09 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Tuesday, January 01, 2008

If you are converting a GUID to a String in C++ you can use this Windows API:

TCHAR szString[41];
::StringFromGUID2(guid, szString, 41);

Note that C++ adds brackets around the GUID so it appears like this:

{c200e360-38c5-11ce-ae62-08002b2b79ef}

However, if you are doing a ToString() in C# you don't get the ending brackets, for example:

String output = Guid.NewGuid().ToString();

Will return:

c200e360-38c5-11ce-ae62-08002b2b79ef

C# will parse a string into a GUID regardless of the brackets.  For example, both these get you the same GUID:

System.Guid guid = new System.Guid("c200e360-38c5-11ce-ae62-08002b2b79ef");

System.Guid guid2 = new System.Guid("{c200e360-38c5-11ce-ae62-08002b2b79ef}");

The equivalent function in C++ is:

::CLSIDFromString(sz,&guid);

However, CLSIDFromtString will not accept a GUID without brackets.  Note here that CLSID, IID and GUID are the same in C++.

If you are going between C++ and C# and you are passing GUIDs as string you need to include the brackets.  It is easier to have C# add the brackets then C++, since it requires a separate string.  Here is how you format your C# string:

String output = String.Format("{{{0}}}",Guid.NewGuid());

Note that you need to use double brackets to represent one bracket on each side.

C# | C++ | GUID | Wayne
Tuesday, January 01, 2008 10:47:06 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  |