Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 how to check uniqueidentifier

Author  Topic 

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2010-06-03 : 16:09:17
how can i check a value
before inserting to db
that its a uniqueidentifier type?

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-06-03 : 16:34:26
Here is one way, there might be better ways.
DECLARE @Foo TABLE (Val VARCHAR(50))

INSERT @Foo
SELECT '12345'
UNION ALL SELECT CAST(NEWID() AS VARCHAR(36))

SELECT
*,
CASE
WHEN Val LIKE '[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]-[0-9A-F][0-9A-F][0-9A-F][0-9A-F]-[0-9A-F][0-9A-F][0-9A-F][0-9A-F]-[0-9A-F][0-9A-F][0-9A-F][0-9A-F]-[0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]'
THEN 1 ELSE 0
END AS IsGuid
FROM @Foo
Go to Top of Page

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2010-06-03 : 16:40:55
i assume using regexp will be better
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-06-03 : 16:46:55
Yeah probably, here is an article about creating a CLR Procedure so you can use RegEx.
http://msdn.microsoft.com/en-us/magazine/cc163473.aspx
Go to Top of Page

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2010-06-03 : 18:51:24
is there way to do it withought using a clr?
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2010-06-03 : 19:42:21
SQL Server IsGuid() function: http://weblogs.sqlteam.com/mladenp/archive/2005/08/16/7545.aspx

also be sure to read the comments.

___________________________________________________________________________
Causing trouble since 1980
Blog: http://weblogs.sqlteam.com/mladenp
Speed up SSMS development: www.ssmstoolspack.com <- version 1.8 out!
Go to Top of Page
   

- Advertisement -