I have a table which has a NVARCHAR column, which could contain a GUID or a string. This is a legacy table, so I can’t change at this point of time.
I need to pick up the records with GUID. Any suggestions? I am using something like below as of now. Should making the the part after the like operator in the below query as UDF would impact performance or there is a better way?
SELECT *
FROM Table1
WHERE Column1 LIKE '[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]-[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]'
You could create a custom function that tries to validate the data and detect it. I found some thing here for tsql:
http://ask.sqlservercentral.com/questions/23114/best-way-to-check-that-a-stringvarchar-guid-is-val.html
Update:
Check marc_s’s answer here too:
How to check if a string is a uniqueidentifier?