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,033 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

726 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