Solved

update a table based on a query of itself

Posted on 2013-01-28
6
156 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:Kdo
Comment Utility
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
Comment Utility
That gives me
Incorrect syntax near '('.
0
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 39

Expert Comment

by:appari
Comment Utility
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
Comment Utility
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
Comment Utility
That did it and added the new columns all in one statement, Nice!

Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

771 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

10 Experts available now in Live!

Get 1:1 Help Now