How to check if a string is a uniqueidentifier?

前端 未结 12 926
小蘑菇
小蘑菇 2020-11-29 03:06

Is there an equivalent to IsDate or IsNumeric for uniqueidentifier (SQL Server)? Or is there anything equivalent to (C#) TryParse?

Otherwise I\'ll have to write my o

12条回答
  •  一个人的身影
    2020-11-29 03:41

    You can write your own UDF. This is a simple approximation to avoid the use of a SQL-CLR assembly.

    CREATE FUNCTION dbo.isuniqueidentifier (@ui varchar(50))  
    RETURNS bit AS  
    BEGIN
    
    RETURN case when
        substring(@ui,9,1)='-' and
        substring(@ui,14,1)='-' and
        substring(@ui,19,1)='-' and
        substring(@ui,24,1)='-' and
        len(@ui) = 36 then 1 else 0 end
    
    END
    GO
    

    You can then improve it to check if it´s just about HEX values.

提交回复
热议问题