Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
1,083 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 70

Accepted Solution

by:
Scott Pletcher earned 1200 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 800 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

596 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