203

We have a log table that has a message column that sometimes has an exception stack trace. I have some criteria that determines if the message has this. We do not want to show these messages to the customer but instead have a message like:

Internal Error Occured. Contact US with reference code xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

where xxx etc is a guid column in the table. I am writing stored proc like this:

declare @exceptionCriteria nvarchar(50)
select @exceptionCriteria = '%<enter criteria etc>%'

select LogDate,
       case
       when Message like @exceptionCriteria
       then 'Internal Error Occured. Reference Code: ' + str(RequestID)
       else Message
       end
  from UpdateQueue

RequestID is a Guid datatype in SQL Server and does not convert to string here. I've seen some code on how to convert a Guid to string, but it is multi-lined and I don't think it would work in a case statement. Any ideas?

cwharris
  • 17,835
  • 4
  • 44
  • 64
aarona
  • 35,986
  • 41
  • 138
  • 186

4 Answers4

369

I think I found the answer:

convert(nvarchar(36), RequestID)

Here's the link where I found this info:

http://msdn.microsoft.com/en-us/library/ms187928.aspx

aarona
  • 35,986
  • 41
  • 138
  • 186
113

It is possible to use the convert function here, but 36 characters are enough to hold the unique identifier value:

convert(nvarchar(36), requestID) as requestID

Edit: yes, as noted in the comments, char, or nchar, or any function that can properly manipulate ASCII character tables would do the trick. Then, my excuse is that I usually work in a multilingual/multialphabet environment, and the rule is to go for nvarchar, always. That's my no-brainer way of doing things, sorry. And, if one of these days, some database software starts to generate unique identifier with non-ASCII elements, I will be ready.

Philippe Grondier
  • 10,900
  • 3
  • 33
  • 72
  • 2
    because length is fixed even varchar(36) is enough here – gdbdable Apr 20 '16 at 12:49
  • 14
    You say "fixed" and yet "varchar" in the same sentence ... how about `char(36)`? You might also use `nchar(36)`, but since a GUID doesn't contain unicode, it buys you nothing. Conversely, operations with `char` are generally faster than `varchar`. – r2evans Jun 09 '17 at 15:00
  • Shouldn't we be uppercase CONVERT – P6345uk Apr 28 '21 at 08:27
72

In my opinion, uniqueidentifier / GUID is neither a varchar nor an nvarchar but a char(36). Therefore I use:

CAST(xyz AS char(36))
d219
  • 2,707
  • 5
  • 31
  • 36
Silvan Hofer
  • 1,361
  • 11
  • 12
9

Instead of Str(RequestID), try convert(varchar(38), RequestID)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Did you mean `varchar(36)`? What accounts for the additional two characters when we're dealing with a GUID? – Jeremy Caney Apr 25 '22 at 21:44
  • 1
    @JeremyCaney I edited my answer to show a better solution. This was a long time ago, but I think the column in the database had a 50 character length so this is why I had chosen that value. – aarona Apr 26 '22 at 17:03