Solved

Replacing names in a database using SQL script

Posted on 2014-10-03
2
45 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach 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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

910 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

17 Experts available now in Live!

Get 1:1 Help Now