Just something a bit strange, when you insert a GUID into SQL Server, the letters are lower case. See the following. Fire up Visual Studio 2010 and run the following c# console app in debug mode:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer;
using System.Data.SqlClient;
 
namespace GUIDTest
{
    class Program
    {
        static void Main(string[] args)
        {        
           using (SqlConnection myConnection = 
                    new SqlConnection("integrated security=SSPI;" +
                    "password=password;server=;" +
                    "Trusted_Connection=yes;" +
                    "database=test; " +
                    "connection timeout=30"))
 
            {
 
                try
                {
                 myConnection.Open();
 
                 Guid param = Guid.NewGuid();
 
                 SqlCommand myCommand 
                   = new SqlCommand ("INSERT INTO GUIDTest (id, guidcol) Values (1, @param)"
, myConnection);
 
                 myCommand.Parameters.AddWithValue("@param", param);
                 myCommand.ExecuteNonQuery();
 
                 SqlCommand myCommand2 
                     = new SqlCommand("SELECT guidcol FROM GUIDTest WHERE guidcol = '"
+ param + "'", myConnection);
                // var value = myCommand2.ExecuteScalar();
                Guid value = (Guid) myCommand2.ExecuteScalar();
                }
                catch (SqlException e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
    }
}

 

image

Notice that the GUID string has letters and numbers, all letters are lower case for sure. If I select back out the same GUID that I generated and inserted, it looks like the statement still returns lower case letters in the GUID:

 

image

Now go into SQL Server and take a look at the representation for the GUID that is returned back.


image


Notice that the letters within the uniqueidentifier are capital letters now.   Someone here suggested that the string representation is caps; however, if I cast the value to a uniqueidentifier I still get the same behavior

image

I’ve not worked much with GUIDS, and never really wanted to, but because I’m working on a database where they’re used quite a bit I have to learn to deal with them. Unfortunately one of the databases here is full of them…still looking for the idiot who designed this so I can punch them LOL. Goes to show that you have to learn it all because you never know what awaits you in a given environment.

This little phenomena could cause problems of course, and we had one here. We’re encrypting components within a credit card table, including the credit card and the account number, which is of uniqueidentifier type. So, what arose was that SQL Server dishes up the caps, but Visual Studio doesn’t, and in the encrypt/decrypt process, we found the obvious that these took on different values, so we had to make changes to the representation of the GUID. This may be all over Google, but I searched a few times and didn’t find much on it.

Thanks,
Lee


-------------------------

Maybe because everyone else is smart enough to stay away from the damn things


Posted in: TSQL  Tags:
blog comments powered by Disqus

by Lee Everest, M.S.

Poll

Do you use Azure or cloud in your organization?



Show Results

Ads

Search


Month List

Calendar

«  February 2012  »
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011
View posts in large calendar

Tags

Disclaimer
The opinions, code, examples, et.al. expressed herein are my own personal opinions and do not represent my employer's view in any way, shape form, or fashion.  All code for demonstration purposes - no guarantees, either written or implied, are made.

© Copyright 2012 Lee Everest's SQL Server, etc. weblog