Showing posts with label C#.Net. Show all posts
Showing posts with label C#.Net. Show all posts

Saturday, 8 February 2014

How to convert the contents of a Column to Proper case/ Title Case

If for any reason you want to change the text data that is stored in the database to Title case, follow the following steps:

1. In Microsoft SQL Server Management Studio, Select the Database and Create a New Query

Now Paste the following code. This will create a function.
CREATE FUNCTION dbo.TitleCaseText(@input NVARCHAR(4000)) RETURNS NVARCHAR(4000)
AS 
BEGIN
  DECLARE @position INT
    WHILE IsNull(@position,Len(@input)) > 1
     SELECT @input = Stuff(@input,IsNull(@position,1),1,upper(substring(@input,IsNull(@position,1),1))), 
@position = charindex(' ',@input,IsNull(@position,1)) + 1
RETURN (@input)
END

2. To just display the contents of the column in Title case, use the following syntax
SELECT dbo.TitleCaseText(ColumnName) FROM TableName


3. To update the contents of the column to Title Case, use the following syntax
UPDATE TableName SET ColumnName = dbo.TitleCaseText(ColumnName)