Solved

Need help converting 2 columns to proper case in SQL Server 2005.

Posted on 2007-04-09
3
186 Views
Last Modified: 2010-03-19
I have 2 columns in an SQL database table, LastName, and FirstName. The information entered is all mixed case. I have i.e. JOHN SMITH,  jane doe, John Doe. Is there any way for me to run a query on  those 2 columns and correct the case to proper case? I would also like to create a backup plan, like copy this table and rename it, just in case the original table gets toasted accidentally.
0
Comment
Question by:CementTruck
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Expert Comment

by:dready
ID: 18878015
Hi,

here you can find a user defined function to Propercase a string:

http://pscode.com/vb/scripts/ShowCode.asp?txtCodeId=780&lngWId=5

you can use it in this form

update myTable set LastName = fn_PROPERCASE_All(LastName), firstName = fn_PROPERCASE_All(FirstName).

To make a backup of your tables, you can do

SELECT * INTO myTable_backup
FROM myTable

this will create the table myTable_backup and copy all the data there.

Hope this helps.
0
 
LVL 10

Accepted Solution

by:
ksaul earned 500 total points
ID: 18878273
You could create a function like
ALTER FUNCTION ProperCase
        (@text varchar(256))
RETURNS varchar(256)
AS

BEGIN
declare @counter int,
      @newtext varchar(256)
SET @counter = 2
SET @text = ltrim(rtrim(@text))
set @newtext = UPPER(SUBSTRING(@text,1,1))

WHILE @counter <= len(@text)
        BEGIN
                  IF SUBSTRING(@text,@counter - 1,1) = ' '
                        OR SUBSTRING(@text,@counter - 2,2) = 'mc'
                        OR SUBSTRING(@text,@counter - 2,2) = ' o'
                        SET @newtext = @newtext + UPPER(SUBSTRING(@text, @counter,1))
                  ELSE
                        SET @newtext = @newtext + LOWER(SUBSTRING(@text, @counter,1))
                  SET @counter = @counter + 1
        END
finish:
RETURN @newtext
END

"Proper Case" can vary.  If it is for names is hard to get it exactly right (names like terHorst, Van de Graff, de la Garza... )  And titles have rules for words that are always lower case.  You can modify the function to include the rules you want.

Once you have a function that suits your needs you can test it like:

SELECT FirstName, LastName, dbo.ProperCase(FirstName), dbo.ProperCase(LastName)
FROM YourTable

Then backup the table with:
SELECT *
INTO YourTableBackup
FROM YourTable

Then update with
UPDATE YourTable
SET FirstName = dbo.ProperCase(FirstName),
    LastName =  dbo.ProperCase(LastName)
0
 
LVL 10

Expert Comment

by:ksaul
ID: 18950638
Did either of these two suggestions work for you?
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHOWPLAN permission denied in database 'AdventureWorks'. 13 119
Isolation level in SQL server 3 58
INSERT DATE FROM STRING COLUMN 18 66
Query to return total 6 26
There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question