Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 632 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL Server: converting UniqueIdentifier to string in a case statement

#1
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?
Reply

#2
Instead of `Str(RequestID)`, try `convert(varchar(38), RequestID)`
Reply

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

CAST(xyz AS char(36))
Reply

#4
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.
Reply

#5
I think I found the answer:

convert(nvarchar(36), RequestID)

Here's the link where I found this info:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through