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

x
?
Solved

Replacing names in a database using SQL script

Posted on 2014-10-03
2
Medium Priority
?
56 Views
Last Modified: 2015-02-19
Hi,

I have an issue where all of the names in a column that have a middle name have the middle name and last name together without a space. So it appears, First MiddleLast. I have a file that I can import into another table that has all of these names in their proper format. All names are associated with a unique number from an adjacent column, so I'm wondering if there was another script that I can utilize that's dynamic enough so that I don't need to specify each old name and new name 3000 times. I've used this one below in the past for small batches, but this time the number is large enough that I'm thinking that there has to be a better way.

UPDATE TableName
SET Name= 'First Middle Last'
WHERE Name = 'First MiddleLast';

Thanks in advance,

Brian
0
Comment
Question by:Whah
[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 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 40360586
Why not retain the 3 separate fields, and use a "computed column" for the concatenation of those?

This gives you much more flexibility.

e.g.
ALTER TABLE Employees
ADD FullName as (replace(isnull(First,'') + ' ' + isnull(Middle,'') + ' ' + isnull(Last,''),'  ',' '))
;

oh, & if the method of concatenation is wrong there just one place to go and fix it.

Please read the documentation if unfamiliar with computed columns: e.g.
http://msdn.microsoft.com/en-us/library/ms188300(v=sql.105).aspx

-----------

regarding an update:

update employees
set employees.name = replace(isnull(source.First,'') + ' ' + isnull(source.Middle,'') + ' ' + isnull(source.Last,''),'  ',' ')
from employees e
inner join temp_Employees AS source on e.empno = source.empno
;

Open in new window

-----------
see BOTH methods working here: http://sqlfiddle.com/#!3/7d2d7/1

details of that:
    CREATE TABLE Employees
    	([empno] int, [First] varchar(20), [Middle] varchar(20), [Last] varchar(20), [Name] varchar(60))
    ;
    	
    INSERT INTO Employees
    	([empno], [First], [Middle], [Last])
    VALUES
    	(200100354, 'aaron', 'mike', 'dingbat'),
    	(200100354, 'bart', 'neil', 'dingbat'),
    	(200100359, 'charlie', NULL, 'dingbat'),
    	(200100362, 'alyson', 'michelle', 'dingbat'),
    	(200100605, 'barbara', 'nellie', 'dingbat'),
    	(200100683, 'carol', NULL, 'dingbat')
    ;
    
    ALTER TABLE Employees
    ADD FullName as (replace(isnull(First,'') + ' ' + isnull(Middle,'') + ' ' + isnull(Last,''),'  ',' '))
    ;
    
    
    CREATE TABLE temp_Employees
    	([empno] int, [First] varchar(20), [Middle] varchar(20), [Last] varchar(20))
    ;
    	
    INSERT INTO temp_Employees
    	([empno], [First], [Middle], [Last])
    VALUES
    	(200100354, 'aaron', 'mike', 'dingbat'),
    	(200100354, 'bart', 'neil', 'dingbat'),
    	(200100359, 'charlie', NULL, 'dingbat'),
    	(200100362, 'alyson', 'michelle', 'dingbat'),
    	(200100605, 'barbara', 'nellie', 'dingbat'),
    	(200100683, 'carol', NULL, 'dingbat')
    ;

**Query 1**:

    select
    *
    from employees
    

**[Results][2]**:
    
    |     EMPNO |   FIRST |   MIDDLE |    LAST |   NAME |                FULLNAME |
    |-----------|---------|----------|---------|--------|-------------------------|
    | 200100354 |   aaron |     mike | dingbat | (null) |      aaron mike dingbat |
    | 200100354 |    bart |     neil | dingbat | (null) |       bart neil dingbat |
    | 200100359 | charlie |   (null) | dingbat | (null) |         charlie dingbat |
    | 200100362 |  alyson | michelle | dingbat | (null) | alyson michelle dingbat |
    | 200100605 | barbara |   nellie | dingbat | (null) |  barbara nellie dingbat |
    | 200100683 |   carol |   (null) | dingbat | (null) |           carol dingbat |


**Query 2**:

    update employees
    set employees.name = replace(isnull(source.First,'') + ' ' + isnull(source.Middle,'') + ' ' + isnull(source.Last,''),'  ',' ')
    from employees e
    inner join temp_Employees source on e.empno = source.empno
    

**[Results][3]**:
    

**Query 3**:

    select
    *
    from employees
    

**[Results][4]**:
    
    |     EMPNO |   FIRST |   MIDDLE |    LAST |                    NAME |                FULLNAME |
    |-----------|---------|----------|---------|-------------------------|-------------------------|
    | 200100354 |   aaron |     mike | dingbat |      aaron mike dingbat |      aaron mike dingbat |
    | 200100354 |    bart |     neil | dingbat |      aaron mike dingbat |       bart neil dingbat |
    | 200100359 | charlie |   (null) | dingbat |         charlie dingbat |         charlie dingbat |
    | 200100362 |  alyson | michelle | dingbat | alyson michelle dingbat | alyson michelle dingbat |
    | 200100605 | barbara |   nellie | dingbat |  barbara nellie dingbat |  barbara nellie dingbat |
    | 200100683 |   carol |   (null) | dingbat |           carol dingbat |           carol dingbat |



  [1]: http://sqlfiddle.com/#!3/7d2d7/1

Open in new window

0
 

Author Closing Comment

by:Whah
ID: 40619911
Thank you for the help.

Brian
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

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