Click here to check my latest exciting videos on youtube
Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment


Locations of visitors to this page


Latest Articles


Move to top
Capitalise first character of every word
Posted By Sarin on Mar 21, 2012     RSS Feeds     Latest Hinduism news
2359 Views


Capitalise first character of every word in input string

This function will work not only for input string, separated by spaces but also by other delimiters like semicolon , comma, colon etc.
You can add,modify or delete this delimietrs as per your requirement.

CREATE
 FUNCTION [InitCap]  ( @InputString varchar(4000))
 RETURNS VARCHAR(4000) AS
 BEGIN  
  
DECLARE @Index INT  
  
DECLARE @Char CHAR(1)
 DECLARE @PrevChar CHAR(1)  
  
DECLARE @OutputString VARCHAR(255)
 SET @OutputString = LOWER(@InputString)  
  
SET @Index = 1  
  
WHILE @Index <= LEN(@InputString)
   BEGIN    
   
SET @Char= SUBSTRING(@InputString, @Index, 1)  
   
SET @PrevChar = CASE WHEN @Index = 1 THEN ' '  
                         
ELSE SUBSTRING(@InputString, @Index - 1, 1)  
   
END  
   
IF @PrevChar IN  (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')    
     
BEGIN    
       
IF @PrevChar != '''' OR UPPER(@Char) != 'S'  
         
SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))    
       
END      
       
SET @Index = @Index + 1
     
END  
   
RETURN @OutputString
 END

Description:
  
1)    First, we convert the whole input string into lowercase.
2)    For each character of the input string, Following steps are performed
  
    For each character, we check if the previous character was some special character like  (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')    
      
    •  
    • If yes, then we check if there is a special case of apostrophe followed by s. For Ex: Product’s             (If we don’t check this case, then input substring Product’s will be displayed as Product’S which we don’t want as per our requirement)   
    • If above check is false, then we Replace the current letter with its uppercase equivalent.  
      • Increment @Index value so that @Char point to next character in next loop.
              3)    Return the output string.
    

Note: Images used on this website are either a production of Bhaktivedanta Book Trust(https://www.krishna.com), Iskcon Foundation or were found in google search under "Free to use and share". If any of the images presented here violates copyright issues or infringes anyone copyright or are not under "Fair use", then please bring it to our notice. Read Disclaimer for more.

Share this to your friends. One of your friend is waiting for your share.
Share on Google+ Share on Tumblr Print Browser Favorite
Related Articles
Different string functions in SQL-Part 1
Commonly used string operation in SQL
Advanced Strings Format for numbers
Create property class of database table
Advanced Datetime Format for string
Ways of generating random password or string
Capitalise first character of every word
Need and advantages of using computed columns
Parse comma separated input string in SQL
Different string functions in SQL-Part 2

Post Comment