Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples SQL Server
 

Change to Proper case Function


Code ResourceAuthor: Jim.Anderson  

Difficulty Level:

Published: 6th Nov 2006   Read: 1729 times  

Filed in: SQL Server
Add Comment


 


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 Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





Popular Coders

# Coder NameHits
1. Jim.Anderson1730
2. gupt_121719

Active Coders

# Coder NameCodes
1. gupt_121
2. Jim.Anderson1

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape