<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: How does SQL Anywhere sort Guids / UniqueIdentifiers in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819757#M4850600</link>
    <description>&lt;P&gt;In 12.0.1.3298 columns with the UNIQUEIDENTIFIER data type are sorted by ORDER BY just like character strings.&lt;/P&gt;
&lt;P&gt;Under the hood, they may be treated as BINARY but the actual data only contains 0-9 and a-e so the values might as well be VARCHAR... no collation or character set worries, methinks.&lt;/P&gt;
&lt;P&gt;Here's proof...&lt;/P&gt;
&lt;PRE&gt;CREATE TABLE t ( 
    pkey   INTEGER          NOT NULL PRIMARY KEY,
    guid   UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(),
    string VARCHAR ( 36 )   COMPUTE ( UUIDTOSTR ( guid ) ) );

INSERT t ( pkey ) SELECT row_num FROM sa_rowgenerator ( 1, 10 );
COMMIT;

SELECT pkey, guid FROM t ORDER BY guid;
SELECT pkey, string FROM t ORDER BY string;

pkey        guid                                   
----------- -------------------------------------- 
2           0x2a353f246c5c43948bd0f969db3dc53b     
1           0x3547a2aa04d442b2b523f8f4da468c80     
3           0x4d99bbd3aaf64fb6ae3699050927d0ab     
6           0x55530a30c4204ddfb56a367b4ee81026     
10          0x5b3d2eab6aa64ce3bc3bc640ac4ed579     
7           0x5ccf0d35616e464e8992ba0ac773cf1b     
8           0xb540fe6d9b15495db321202083a9e17a     
4           0xe03acc02cc5846daa713866354befbdb     
9           0xe9ab069d0cd84302ad54a72812694a4b     
5           0xef875058e8274d099c57e93c297c1902

pkey        string                               
----------- ------------------------------------ 
2           2a353f24-6c5c-4394-8bd0-f969db3dc53b 
1           3547a2aa-04d4-42b2-b523-f8f4da468c80 
3           4d99bbd3-aaf6-4fb6-ae36-99050927d0ab 
6           55530a30-c420-4ddf-b56a-367b4ee81026 
10          5b3d2eab-6aa6-4ce3-bc3b-c640ac4ed579 
7           5ccf0d35-616e-464e-8992-ba0ac773cf1b 
8           b540fe6d-9b15-495d-b321-202083a9e17a 
4           e03acc02-cc58-46da-a713-866354befbdb 
9           e9ab069d-0cd8-4302-ad54-a72812694a4b 
5           ef875058-e827-4d09-9c57-e93c297c1902 
&lt;/PRE&gt;</description>
    <pubDate>Thu, 11 Jul 2013 11:13:05 GMT</pubDate>
    <dc:creator>Breck_Carter</dc:creator>
    <dc:date>2013-07-11T11:13:05Z</dc:date>
    <item>
      <title>How does SQL Anywhere sort Guids / UniqueIdentifiers</title>
      <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaq-p/13819756</link>
      <description>&lt;P&gt;We have a number of tables in our database that contain &lt;CODE&gt;UNIQUEIDENTIFIER&lt;/CODE&gt; columns.  The question has arisen as to exactly how these are sorted, especially if you add an index on such a column.  In particular, how does the sorting of this type differ, if at all, from how C# might sort them.&lt;/P&gt;
&lt;P&gt;The documentation says that these values are stored as BINARY(16).  I guess that they would be sorted by taking the 16 bytes in the order they appear in memory / disk and comparing them as a string of bytes.  But is the order of the bytes in the BINARY(16) the same as the order in which the digits appear when you convert the &lt;CODE&gt;UNIQUEIDENTIFIER&lt;/CODE&gt; to a string?  My understanding is .NET does not store the bytes in that order, so they don't sort in that order, either.&lt;/P&gt;
&lt;P&gt;So how does SQL Anywhere 12.0.1.3895 sort &lt;CODE&gt;UNIQUEIDENTIFIERS&lt;/CODE&gt;?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 10:12:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaq-p/13819756</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2013-07-11T10:12:44Z</dc:date>
    </item>
    <item>
      <title>Re: How does SQL Anywhere sort Guids / UniqueIdentifiers</title>
      <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819758#M4850601</link>
      <description>&lt;P&gt;You ask the d*****est questions! &lt;/P&gt;
&lt;P&gt;Let's add a third question, along with "How does SQL Anywhere sort GUIDs?" and "How does .NET sort GUIDs?" &lt;/P&gt;
&lt;P&gt;Danger! Will Robinson! Danger!&lt;/P&gt;
&lt;P&gt;&lt;A href="http://sqlblog.com/blogs/alberto_ferrari/archive/2007/08/31/how-are-guids-sorted-by-sql-server.aspx"&gt;How does SQL Server sort GUIDs?&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 10:45:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819758#M4850601</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2013-07-11T10:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: How does SQL Anywhere sort Guids / UniqueIdentifiers</title>
      <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819759#M4850602</link>
      <description>&lt;P&gt;Actually, our system architect asked the question.  I figured it was worth asking here. &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 10:47:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819759#M4850602</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2013-07-11T10:47:39Z</dc:date>
    </item>
    <item>
      <title>Re: How does SQL Anywhere sort Guids / UniqueIdentifiers</title>
      <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819757#M4850600</link>
      <description>&lt;P&gt;In 12.0.1.3298 columns with the UNIQUEIDENTIFIER data type are sorted by ORDER BY just like character strings.&lt;/P&gt;
&lt;P&gt;Under the hood, they may be treated as BINARY but the actual data only contains 0-9 and a-e so the values might as well be VARCHAR... no collation or character set worries, methinks.&lt;/P&gt;
&lt;P&gt;Here's proof...&lt;/P&gt;
&lt;PRE&gt;CREATE TABLE t ( 
    pkey   INTEGER          NOT NULL PRIMARY KEY,
    guid   UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(),
    string VARCHAR ( 36 )   COMPUTE ( UUIDTOSTR ( guid ) ) );

INSERT t ( pkey ) SELECT row_num FROM sa_rowgenerator ( 1, 10 );
COMMIT;

SELECT pkey, guid FROM t ORDER BY guid;
SELECT pkey, string FROM t ORDER BY string;

pkey        guid                                   
----------- -------------------------------------- 
2           0x2a353f246c5c43948bd0f969db3dc53b     
1           0x3547a2aa04d442b2b523f8f4da468c80     
3           0x4d99bbd3aaf64fb6ae3699050927d0ab     
6           0x55530a30c4204ddfb56a367b4ee81026     
10          0x5b3d2eab6aa64ce3bc3bc640ac4ed579     
7           0x5ccf0d35616e464e8992ba0ac773cf1b     
8           0xb540fe6d9b15495db321202083a9e17a     
4           0xe03acc02cc5846daa713866354befbdb     
9           0xe9ab069d0cd84302ad54a72812694a4b     
5           0xef875058e8274d099c57e93c297c1902

pkey        string                               
----------- ------------------------------------ 
2           2a353f24-6c5c-4394-8bd0-f969db3dc53b 
1           3547a2aa-04d4-42b2-b523-f8f4da468c80 
3           4d99bbd3-aaf6-4fb6-ae36-99050927d0ab 
6           55530a30-c420-4ddf-b56a-367b4ee81026 
10          5b3d2eab-6aa6-4ce3-bc3b-c640ac4ed579 
7           5ccf0d35-616e-464e-8992-ba0ac773cf1b 
8           b540fe6d-9b15-495d-b321-202083a9e17a 
4           e03acc02-cc58-46da-a713-866354befbdb 
9           e9ab069d-0cd8-4302-ad54-a72812694a4b 
5           ef875058-e827-4d09-9c57-e93c297c1902 
&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jul 2013 11:13:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819757#M4850600</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2013-07-11T11:13:05Z</dc:date>
    </item>
    <item>
      <title>Re: How does SQL Anywhere sort Guids / UniqueIdentifiers</title>
      <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819760#M4850603</link>
      <description>&lt;P&gt;...an excellent example of the Watcom Rule. What &lt;EM&gt;possible&lt;/EM&gt; reason could there be for different behavior? Clearly, Microsoft engineers have way too much time on their hands... they invented ODBC, what more proof must they provide?&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 11:16:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819760#M4850603</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2013-07-11T11:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: How does SQL Anywhere sort Guids / UniqueIdentifiers</title>
      <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819761#M4850604</link>
      <description>&lt;P&gt;"worth asking" - oh yes, as Alberto Ferrari pointed out in that link, sorting-for-comparison-purposes is an important process.&lt;/P&gt;
&lt;P&gt;As far as indexes are concerned, I imagine UNIQUEIDENTIFIER is a pretty good random hash, if that's what you want for your index; i.e., good for equality searches, sucks for range queries.&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2013 11:20:25 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819761#M4850604</guid>
      <dc:creator>Breck_Carter</dc:creator>
      <dc:date>2013-07-11T11:20:25Z</dc:date>
    </item>
    <item>
      <title>Re: How does SQL Anywhere sort Guids / UniqueIdentifiers</title>
      <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819762#M4850605</link>
      <description>&lt;P&gt;Breck...Thanks for the answer.  I really wasn't expecting anything different, but I couldn't find anything in the documentation about it, and a Google search came back empty.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jul 2013 08:30:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819762#M4850605</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2013-07-12T08:30:52Z</dc:date>
    </item>
    <item>
      <title>Re: How does SQL Anywhere sort Guids / UniqueIdentifiers</title>
      <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819763#M4850606</link>
      <description>&lt;P&gt;About the only thing a UNIQUEIDENTIFIER is good for is uniquely identifying a record.  So what use is a range query with a UNIQUEIDENTIFIER key anyway?&lt;/P&gt;
&lt;P&gt;Anyway, thanks again for the answer.&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jul 2013 08:37:59 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819763#M4850606</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2013-07-12T08:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: How does SQL Anywhere sort Guids / UniqueIdentifiers</title>
      <link>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819764#M4850607</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;a Google search came back empty.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That might be different in a while - thanks to Breck - and to your question:)&lt;/P&gt;</description>
      <pubDate>Fri, 12 Jul 2013 08:38:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/how-does-sql-anywhere-sort-guids-uniqueidentifiers/qaa-p/13819764#M4850607</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2013-07-12T08:38:51Z</dc:date>
    </item>
  </channel>
</rss>

