I want some Moore

Blog about stuff and things and stuff. Mostly about SQL server and .Net
posts - 218, comments - 2281, trackbacks - 33

My Links

Advertisement

News

Hi! My name is 
Mladen Prajdić  I'm from Slovenia and I'm currently working as a .Net (C#) and SQL Server developer.

I also speak at local user group meetings and conferences like SQLBits and NT Conference
Welcome to my blog.
SQL Server MVP

My Books

SQL Server MVP Deep Dives 2
The Red Gate Guide to SQL Server Team based Development Free e-book

My Blog Feed via Email
Follow MladenPrajdic on Twitter


Users Online: who's online

Article Categories

Archives

Post Categories

Cool software

Other Blogs

Other stuff

SQL stuff

SQL Server IsGuid() function

A simple function that checks if a passed string is a GUID.
create function IsGuid ( @testString varchar(38))
returns int
as
begin
    declare @ret int
    select  @ret = 0,
            @testString = replace(replace(@testString, '{', ''), '}', '')
    if len(isnull(@testString, '')) = 36 and
       @testString NOT LIKE '%[^0-9A-Fa-f-]%' and
       -- check for proper positions of hyphens (-)  
       charindex('-', @testString) = 9 and 
       charindex('-', @testString, 10) = 14 and 
       charindex('-', @testString, 15) = 19 and 
       charindex('-', @testString, 20) = 24 and 
       charindex('-', @testString, 25) = 0
          set @ret = 1
    
    return @ret
end
go
select dbo.IsGuid ('DAB1df7F-BCB8-4935-9364-f140F64EE5F7')
select dbo.IsGuid ('{DAB1df7F-BCB8-4935-9364-f140F64EE5F7}')
go
drop function dbo.IsGuid

Print | posted on Tuesday, August 16, 2005 5:02 PM | Filed Under [ SQL Server ]

Feedback

Gravatar

# re: SQL Server IsGuid() function

Here's another style using replicate :)


[code]
create function IsGuid ( @testString varchar(38))
returns int
as
begin
declare @ret int

select @ret = 0,
@testString = replace(replace(@testString, '{', ''), '}', '')


Select @ret = case when @testString like
(replicate('[0-9A-Fa-f]',8) + '-' +
replicate('[0-9A-Fa-f]',4) + '-' +
replicate('[0-9A-Fa-f]',4) + '-' +
replicate('[0-9A-Fa-f]',4) + '-' +
replicate('[0-9A-Fa-f]',12)) then 1 else 0 end

return @ret
end
go
select dbo.IsGuid ('DAB1df7F-BCB8-4935-9364-f140F64EE5F7')
select dbo.IsGuid ('{DAB1df7F-BCB8-4935-9364-f140F64EE5F7}')
select dbo.IsGuid ('3DAB1df7F-BCB8-4935-9364-f140F64EE5F7')
go
drop function dbo.IsGuid
[/code]
3/31/2006 4:54 PM | Seventhnight
Gravatar

# re: SQL Server IsGuid() function

good one!
3/31/2006 5:03 PM | mladen
Gravatar

# re: SQL Server IsGuid() function

very good!
8/2/2007 7:41 AM | sanjoy
Gravatar

# re: SQL Server IsGuid() function

Very useful! Thank you very much
9/5/2007 3:25 AM | malik
Gravatar

# re: SQL Server IsGuid() function

Cool!
Thank you very much
3/12/2008 9:42 AM | Fox
Gravatar

# re: SQL Server IsGuid() function

Cool!
Thank you very much
3/12/2008 9:42 AM | Fox
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET