Solved

SQL Server Help with Update Statement

Posted on 2014-02-20
2
241 Views
Last Modified: 2014-02-20
Hi.. I need to clean up data in a LASTNAME field.
I need to find and replace all the 'ii' and replace with 'II' and find all the 'iii' and replace with 'III'



The data looks like this

jones  ii
smith iii
brown
green ii


Need it look like this
Jones II
Smith III
Brown
Green II


Basically capitalize the '2nd' and '3rd'
0
Comment
Question by:JElster
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 400 total points
ID: 39874459
Check the Replace Function: http://technet.microsoft.com/en-us/library/ms186862.aspx

The space before the string to replace should make sure it picks it up at the end of the string, incase there's a last name that has ii or iii as a part of the actual name and not the suffix.

update table1
set lastname = replace(lastname, ' iii', ' III');

update table1
set lastname = replace(lastname, ' ii', ' II');

Open in new window


You can check what the output of the update would look like before the update by doing:

select lastname, replace(lastname, ' ii', ' II') from table1;

Open in new window

0
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 100 total points
ID: 39874651
UPDATE	yourtable
SET	lastname = replace(replace(lastname, ' iii', ' III'), ' ii', ' II')
WHERE	lastname like '%ii%'

Open in new window

0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

730 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