简体   繁体   中英

Select specific column values in SQL Server

I have a SQL query which result me 7 rows under one column ie 'COMPONENT' in this manner:

D-SAND
10 MM
20 MM
MSRC
SRC
WATER
SP 607

I want to select all these component into 1 row and just display in one report each time .... Is there any workaround for this please help.

D-SAND 10 MM 20 MM MSRC SRC WATER SP 607

You can use FOR XML PATH('') to get desired result.

SELECT LEFT(x.COMP.value('.', 'NVARCHAR(MAX)'), LEN(x.COMP.value('.', 'NVARCHAR(MAX)')) -1) as COMPONENT
FROM
(
    SELECT COMPONENT+', '
    FROM yourTable
    FOR XML PATH(''), TYPE
) x(COMP);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM