Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-09
3
Medium Priority
?
189 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

670 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