Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Updating records to be in correct case.

Author  Topic 

Tekhne
Starting Member

3 Posts

Posted - 2005-07-11 : 17:51:24
I have multiple records that are in all upper case letters. I'd like to convert these to be upper case only on the first letter of each word and lowercase on the rest.

Ex:

I WOULD LIKE IT IF YOU COULD HELP ME

to be...

I Would Like It If You Could Help Me


Any advice would be appreciated.

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-07-11 : 19:21:17
There are lots of ways to accomplish this. Here is one using a function:


CREATE FUNCTION dbo.fn_format_case (@text_in VARCHAR(200))
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @char VARCHAR(1),
@text_out VARCHAR(200),
@pos INT

If @text_in IS NOT NULL
BEGIN
SELECT @char = '',
@text_out = '',
@pos = 1,
@text_in = UPPER(@text_in)
WHILE @pos <= DATALENGTH(@text_in)
BEGIN
-- set @char to first character
SET @char = SUBSTRING(@text_in, @pos, 1)

-- if previous letter is already UPPER then lowercase current pos
IF ASCII(SUBSTRING(@text_in, @Pos - 1, 1)) Between 65 And 90
BEGIN
SET @char = LOWER(@char)
END

-- build output string, increment position
SET @text_out = (@text_out + @char)
SET @pos = (@pos + 1)
END


END
RETURN(@text_out)
END


Nathan Skerl
Go to Top of Page
   

- Advertisement -