Solved

sql to concatenate info from 3 columns on one table and insert data into a new column in a different table

Posted on 2014-03-26
3
1,017 Views
Last Modified: 2014-04-02
I am trying to write a query that will concatenate the info from 3 columns into a new table.

Table1
MasterCustID
Column1
Column2

Table2
MasterCustID
Column3

Table3
MasterCustID
NewCombinedColumn

Such that NewCombinedColumn will end up with the value of
Column1 + '<p>' + Column2 + '</p><p>' + Column3 + '</p>'

All tables are linked by the MasterCustID.
0
Comment
Question by:PurpleSlade
3 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 300 total points
ID: 39957722
--INSERT INTO Table3 ( MasterCustID, NewCombinedColumn )
SELECT
    Table1.MasterCustID,
    Table1.Column1 + '<p>' + Table1.Column2 + '</p><p>' + Table2.Column3 +'</p>' AS NewCombinedColumn
FROM Table1
INNER JOIN Table2 ON
    Table2.MasterCustID = Table1.MasterCustID
0
 
LVL 35

Assisted Solution

by:David Todd
David Todd earned 200 total points
ID: 39960496
Hi,

Might need each field to be wrapped with isnull()

Regards
  David
0
 
LVL 2

Author Comment

by:PurpleSlade
ID: 39972650
Thanks for the replies and sorry for the delay - when I went to implement the query I ran into some complications imposed by the system I'm working with, in that primary keys are not automatically generated.  So I unfortunately can't use this logic exactly as is and I'll have to cycle through and generate the PKs one at a time through a stored proc and do the inserts that way.  But the logic is good - and you were correct David that I will have to wrap the fields with isnull() because some of the tables did not have data in the columns.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

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 needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how the fundamental information of how to create a table.

733 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