Site Search:
Sign in | Join | Help
4Penny.net

SQL Server (T-SQL)

Comments and notes on SQL Server 2000, 2005, and T-SQL

Function to 'Proper' case a string

Occasionally I'll need to format a string in SQL with 'Proper' casing. In other words, change 'steve gray' to 'Steve Gray'.

Here is a function to do that

 CREATE  FUNCTION [dbo].[f_FP_Capitalize] 
-- select dbo.f_FP_Capitalize ('steve gray')
( 
@string VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS 
BEGIN


DECLARE @Next INT
WHILE 1=1
       BEGIN
       --find word space followed by lower case letter
       --This makes assumptions about the language
       SELECT @next= 
           PATINDEX('%[^a-zA-Z][abcdefghijklmnopqurstuvwzyz]%',
                       ' <a href="mailto:'+@string">'+@string</a>  collate Latin1_General_CS_AI)
       IF @next =0 BREAK
       SELECT @String = 
           STUFF(@String,@Next,1,UPPER(SUBSTRING(@String,@Next,1)))
       END
RETURN @string
END

Comments

No Comments

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.