Code.GeekInterview.com
 
Code Samples SQL Server
 

Change to Proper case Function


Code ResourceAuthor: Jim.Anderson  

Difficulty Level:

Published: 6th Nov 2006   Read: 5925 times  

Filed in: SQL Server
Add Comment


 

 

Sponsored Links


 

 

This function returns the string in proper case like microsoft word function. Usage : dbo.PROPERCASE(fieldname)

 


Sample Code
  1.  
  2. CREATE FUNCTION PROPERCASE
  3. (
  4. @input VARCHAR(8000)
  5. )
  6. RETURNS VARCHAR(8000)
  7. AS
  8. BEGIN
  9.         IF @input IS NULL
  10.         BEGIN
  11.                                 RETURN NULL
  12.         END
  13.        
  14.        
  15.         DECLARE @output VARCHAR(8000)
  16.                 DECLARE @ctr int, @len int, @found_at int
  17.        
  18.         DECLARE @LOWER_CASE_a int, @LOWER_CASE_z int, @Delimiter CHAR(3), @UPPER_CASE_A int, @UPPER_CASE_Z int
  19.        
  20.        
  21.         SET @ctr = 1
  22.         SET @len = LEN(@input)
  23.         SET @output = ''
  24.         SET @LOWER_CASE_a = 97
  25.         SET @LOWER_CASE_z = 122
  26.         SET @Delimiter = ' ,-'
  27.         SET @UPPER_CASE_A = 65
  28.         SET @UPPER_CASE_Z = 90
  29.        
  30.         WHILE @ctr <= @len
  31.         BEGIN
  32.                
  33.                 WHILE CHARINDEX(SUBSTRING(@input,@ctr,1), @Delimiter) > 0
  34.                 BEGIN
  35.                         SET @output = @output + SUBSTRING(@input,@ctr,1)
  36.                         SET @ctr = @ctr + 1
  37.                 END
  38.  
  39.                 IF ASCII(SUBSTRING(@input,@ctr,1)) BETWEEN @LOWER_CASE_a AND @LOWER_CASE_z
  40.                 BEGIN
  41.                        
  42.                         SET @output = @output + UPPER(SUBSTRING(@input,@ctr,1))
  43.                 END
  44.                 ELSE
  45.                 BEGIN
  46.                         SET @output = @output + SUBSTRING(@input,@ctr,1)
  47.                 END
  48.                
  49.                 SET @ctr = @ctr + 1
  50.  
  51.                 WHILE CHARINDEX(SUBSTRING(@input,@ctr,1), @Delimiter) = 0 AND (@ctr <= @len)
  52.                 BEGIN
  53.                         IF ASCII(SUBSTRING(@input,@ctr,1)) BETWEEN @UPPER_CASE_A AND @UPPER_CASE_Z
  54.                         BEGIN
  55.                                 SET @output = @output + LOWER(SUBSTRING(@input,@ctr,1))
  56.                         END
  57.                         ELSE
  58.                         BEGIN
  59.                                 SET @output = @output + SUBSTRING(@input,@ctr,1)
  60.                         END
  61.                         SET @ctr = @ctr + 1
  62.                 END
  63.                
  64.         END
  65. RETURN @output
  66. END
  67.  
Copyright GeekInterview.com


Next Article: Delete Duplicate Records


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. Jim.Anderson5926
2. gupt_125591

Active Coders

# Coder NameCodes
1. gupt_121
2. Jim.Anderson1

Refined Tags