checking for a uniqueidentifier ms sql

robrodp
robrodp used Ask the Experts™
on
I need to know if a  variable can be hecked to sii if it conforms to a uniqueidentifier
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You should let the sql server create the id for you using NewID()

DECLARE @auniqueid uniqueidentifier
SET @auniqueid = NEWID()

select @auniqueid
Top Expert 2006

Commented:
I am not sure how you can do this within t-sql. What is the datatype you are using to hold this, is it like a binary value?

Only way off hand I can think of is to check the length and dashes appear in the right location.

Have you got a frontend for this db? perhaps it might be easier to perform checking there.

robrodpProgrammer

Author

Commented:
I have a variable and need to know if it can be compared aginst  a uniqueidentity field
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

robrodpProgrammer

Author

Commented:
I use asp
Chief Technology Officer
Most Valuable Expert 2011
Commented:
A uniqueidentifier is 36 characters in length.  4 of those characters are hyphens (-).  32 characters are hexidecimal (0-F) in the following pattern:

12345678-1234-1234-1234-1234567890AB

e.g., 87405730-8A5B-45A9-B63F-AE3139532982

therefore, you can use a regular expression or other such pattern matching in VB like :
^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$

can definitely improve that (correct) the above regex, but hopefully you get the point.

You can also use try / catch around a conversion of the string variable to GUID.  If passes you are fine, if not you will drop into the catch block and you can act accordingly.
robrodpProgrammer

Author

Commented:
Nice and simple

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial