Solved

update a table based on a query of itself

Posted on 2013-01-28
6
161 Views
Last Modified: 2013-01-29
Good Afternoon Experts,

I have a table that is storing a name column as a full name and I want to split it up as first name and last name.  The data is pretty clean, one space between first and last name in each case, no initals etc so I can do a select to seperate the data for example :

SELECT
  SUBSTRING(Loan_Officer, 1, CHARINDEX(' ', Loan_Officer) - 1) AS LO_First_Name,
  SUBSTRING(Loan_Officer, CHARINDEX(' ', Loan_Officer) + 1, 8000) AS LO_Last_Name
FROM
  Custom_LO_Commission

but how do I then take that data and update Custom_LO_Commission table
to insert the two new columns LO_FirstName and LO_LastName with the correct data per record?
0
Comment
Question by:ISBTECH
6 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 38828878
Actually, you've already done the hard work.  :)


UPDATE mytable
SET (firstname, lastname) =
  (SUBSTRING(Loan_Officer, 1, CHARINDEX(' ', Loan_Officer) - 1),
  SUBSTRING(Loan_Officer, CHARINDEX(' ', Loan_Officer) + 1, 8000))
FROM
  Custom_LO_Commission


Good Luck,
Kent
0
 

Author Comment

by:ISBTECH
ID: 38828897
That gives me
Incorrect syntax near '('.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 38828918
You could do that in a AFTER INSERT/UPDATE trigger however my recommandations are to do it in the code that populates the table instead.

You need to put that trigger on the parent table where the INSERT or UPDATE takes place and the code will be something like:

CREATE TRIGGER [dbo].[Trigger_Custom_LO_Commission]
    ON [dbo].Custom_LO_Commission
    AFTER INSERT,UPDATE
AS
BEGIN
SET NOCOUNT ON

UPDATE Custom_LO_Commission
SET LO_First_Name = SUBSTRING(Loan_Officer, 1, CHARINDEX(' ', Loan_Officer) - 1),
      LO_Last_Name  = SUBSTRING(Loan_Officer, CHARINDEX(' ', Loan_Officer) + 1, 8000)
FROM
  Custom_LO_Commission
WHERE Id = (SELECT Id FROM INSERTED)

END;


--where ID from "WHERE Id = (SELECT Id FROM INSERTED)" is the ROW identifier for that Loan_Officer fullname.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 39

Expert Comment

by:appari
ID: 38829468
try this

UPDATE Custom_LO_Commission
SET firstname =    SUBSTRING(Loan_Officer, 1, CHARINDEX(' ', Loan_Officer) - 1) ,
 lastname = SUBSTRING(Loan_Officer, CHARINDEX(' ', Loan_Officer) + 1, 8000)
0
 
LVL 10

Accepted Solution

by:
deviprasadg earned 500 total points
ID: 38830508
Add computed firstname and lastname columns to the table.

ALTER TABLE  Custom_LO_Commission ADD firstname AS SUBSTRING(Loan_Officer, 1, CHARINDEX(' ', Loan_Officer) - 1)
ALTER TABLE  Custom_LO_Commission ADD lastname AS SUBSTRING(Loan_Officer, CHARINDEX(' ', Loan_Officer) + 1, 8000)

Open in new window

0
 

Author Closing Comment

by:ISBTECH
ID: 38830850
That did it and added the new columns all in one statement, Nice!

Thanks!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS - Date Report Options 2 29
How to union three related tables in one flat file? 8 48
SQL server 2014 replication error 35 45
Are triggers slow? 7 14
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

821 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