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
989 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Solution for warm standby SQL server 20 38
Sql Query Datatype 2 18
Sql Query with datetime 3 10
SSRS Dynamic Column Names - Last 8 Weeks 3 5
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
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.
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

910 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

18 Experts available now in Live!

Get 1:1 Help Now