How to check if a string is a uniqueidentifier?

前端 未结 12 924
小蘑菇
小蘑菇 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 04:05

    This is a function based on the concept of some earlier comments. This function is very fast.

    CREATE FUNCTION [dbo].[IsGuid] (@input varchar(50))  
        RETURNS bit AS  
    BEGIN
    
    RETURN 
        case when @input 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]'
        then 1 else 0 end
    END
    GO
    
    /* 
    Usage: 
    
    select [dbo].[IsGuid]('123') -- Returns 0
    select [dbo].[IsGuid]('ebd8aebd-7ea3-439d-a7bc-e009dee0eae0') -- Returns 1
    
    select * from SomeTable where dbo.IsGuid(TableField) = 0 -- Returns table with all non convertable items!
    
    */
    

提交回复
热议问题