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
976 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:
ScottPletcher 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

In this article I will describe the Detach & Attach 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.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

706 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

20 Experts available now in Live!

Get 1:1 Help Now