How to check if a string is a uniqueidentifier?

前端 未结 12 878
小蘑菇
小蘑菇 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:52

    I use :

    ISNULL(convert(nvarchar(50), userID), 'NULL') = 'NULL'
    
    0 讨论(0)
  • 2020-11-29 03:53

    Though an older post, just a thought for a quick test ...

    SELECT  [A].[INPUT],
            CAST([A].[INPUT] AS [UNIQUEIDENTIFIER])
    FROM   (
                SELECT '5D944516-98E6-44C5-849F-9C277833C01B' Collate Latin1_General_100_BIN AS [INPUT]
                UNION ALL
                SELECT '{5D944516-98E6-44C5-849F-9C277833C01B}'
                UNION ALL
                SELECT '5D944516-98E6-44C5-849F-9C277833C01BXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
                UNION ALL
                SELECT '{5D944516-98E6-44C5-849F-9C277833C01B}ssssssssss'
                UNION ALL
                SELECT 'ÀD944516-98E6-44C5-849F-9C277833C01B'
                UNION ALL
                SELECT 'fish'
            ) [A]
    WHERE   PATINDEX('[^0-9A-F-{}]%', [A].[INPUT]) = 0
    
    0 讨论(0)
  • 2020-11-29 03:54
    SELECT something 
      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]';
    

    UPDATE:

    ...but I much prefer the approach in the answer by @r0d30b0y:

    SELECT something 
      FROM table1 
     WHERE column1 LIKE REPLACE('00000000-0000-0000-0000-000000000000', '0', '[0-9a-fA-F]');
    
    0 讨论(0)
  • 2020-11-29 03:59

    I am not aware of anything that you could use "out of the box" - you'll have to write this on your own, I'm afraid.

    If you can: try to write this inside a C# library and deploy it into SQL Server as a SQL-CLR assembly - then you could use things like Guid.TryParse() which is certainly much easier to use than anything in T-SQL....

    0 讨论(0)
  • 2020-11-29 03:59

    A variant of r0d30b0y answer is to use PATINDEX to find within a string...

    PATINDEX('%'+REPLACE('00000000-0000-0000-0000-000000000000', '0', '[0-9a-fA-F]')+'%',@StringToCompare) > 0
    

    Had to use to find Guids within a URL string..

    HTH

    Dave

    0 讨论(0)
  • 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!
    
    */
    
    0 讨论(0)
提交回复
热议问题