Solved

Replacing names in a database using SQL script

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slow Connectivity over ODBC 8 32
How can i use WITH CTE for checking exist value? 3 30
string fuctions 4 25
Sql server function help 15 26
In this article I will describe the Copy Database Wizard 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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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

816 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

11 Experts available now in Live!

Get 1:1 Help Now