Solved

Replacing names in a database using SQL script

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

752 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