Solved

Replacing names in a database using SQL script

Posted on 2014-10-03
2
42 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
2 Comments
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 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

15 Experts available now in Live!

Get 1:1 Help Now