Wednesday, August 8, 2007

Tiny peice of SQL - To find if a sql server guid is a hex

I know guid is a hex, but for very little fun I wrote the following sql to find it out.I got some of the code from Books On Line.Just want to throw it out there to see if something in it helps.


Begin

Declare @Iteration int;
Declare @isHex bit;
Set @Iteration = 1;

WHILE (@Iteration < 100000)
Begin
SET TEXTSIZE 0;
DECLARE @position int;
DECLARE @string char(8);
SET @position = 1;
SET @string = REPLACE(Cast( newid() as varchar(36)),'-','A')
WHILE @position <= DATALENGTH(@string)
BEGIN
Set @isHex =
Case when ASCII(SUBSTRING(@string, @position, 1))> 70 then 0
else 1
End
SET @position = @position + 1;
END
Set @Iteration = @Iteration + 1;
End

If @isHex = 0
Print 'Guid is not a hex';
Else
Print 'Guid is a hex';

End
Go

No comments:

Post a Comment