How to check in SQL if all the characters in a string are capital

Hi,

Many a times we want to check if all the characters in a string are in capital letter or not? Doing this in Dot net is very simple. But what if this kind of checking neds to be done in SQL server. We all know SQL is not the best language to do these kind of checking.

Last Day I had to some similar kind of work. I created a small function to check if all the character in a string(username etc..) are in capital letter or not. Here is the function.

CREATE FUNCTION CheckIfAllCaps (@StringToCheck VARCHAR(500))
RETURNS bit
AS
BEGIN
   DECLARE @return BIT
   DECLARE @position INT

   SET @position = 1

   WHILE @position <= DATALENGTH(@StringToCheck)
   BEGIN
       IF ASCII(SUBSTRINGTOCHECK(@StringToCheck, @position, 1)) BETWEEN 65 AND 90 
           SELECT @return = 0
       ELSE
           SELECT @return = 1

       IF @Return <> 1
           SET @position = @position + 1
       ELSE
           GOTO ExitUDF
END

ExitUDF:
RETURN @return

END

Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia