MS SQL Concatenate multiple values into a single column

Ian Wallace

I currently have the following table;

Invoice   Client   Purchase Order
1000      A1       1234
1000      A1       1235
1001      B2       1236
1001      B2       1237
1002      B2       1238

and I'm looking for a quick way to get to;

Invoice   Client   Purchase Orders
1000      A1       1234 1235
1001      B2       1236 1237
1002      B2       1238

Any help would be appreciated!

Neel Darji

As per your given details, assuming a table #temp below, with sample data:

create table #temp (
     invoice int,
     client varchar(5),
    [purchase order]  int
)

insert into #temp
select 1000,'A1',1234  union all
select 1000,'A1',1235  union all
select 1001,'B2',1236  union all
select 1001,'B2',1237  union all
select 1002,'B2',1238

Now you can use below query using FOR XML as per your required output:

select distinct tp1.invoice,tp1.client,  
(  
    SELECT convert(varchar(10),[purchase order]) + ' ' as [text()]  
    from #temp tp  
    where tp.invoice=tp1.invoice and tp.client=tp1.client  
    for XML path('')  
) as [purchase order]  
from #temp tp1  

If you have any query, let me know.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I concatenate multiple values for a single identifier in SQL?

How to replace multiple values in a single column with SQL?

SQL Query to concatenate column values from multiple rows in Oracle

Concatenate column values from multiple rows in Oracle SQL

Concatenate two column values in SQL

Concatenate column values in SQL Server

sql server multiple column values in single column by building query

SQL select multiple values in single column into multiple columns

Concatenate multiple columns into a list in a single column

SQL Query to combine values from multiple records into a single column

Creating multiple columns from filtered values in single column in SQL

Get multiple values in a single column / row in SQL Server

inserting multiple checkbox values into a single column - SQL database

Comma separated values of multiple columns in single column in sql server

Concatenate rows based on multiple column values

Slicing multiple values into single column

concatenate a string for all column values in sql update

SQL SERVER: concatenate node values of XML column

Pivot and concatenate values from column in SQL Server

Concatenate two column values in Oracle sql

SQL Server concatenate a column's values on subquery

Python convert multiple column values into a single column

Combine Multiple Column Values in a Single Column

MS SQL Store Procedure to Merge Multiple Rows into Single Row based on Variable Table and Column Names

SQL Multiple constraints on a single column

Multiple counts on a single column SQL

Sql single column to multiple on '.' delemiter

How to concatenate two values from different columns into a single column

Concatenate column values of multiple rows based on another column value

TOP Ranking

HotTag

Archive