Solved

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

Posted on 2007-04-09
3
184 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
  • 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now