是否可以编写一个从表中选择列并将结果转换为字符串的语句?
理想情况下,我希望使用逗号分隔值。
例如,假设SELECT语句类似于
SELECT column
FROM table
WHERE column<10
,结果是一个值为
的列|column|
--------
| 1 |
| 3 |
| 5 |
| 9 |
我希望结果是字符串“1,3,5,9”
答案 0 :(得分:49)
你可以这样做:
<强> Fiddle demo 强>
declare @results varchar(500)
select @results = coalesce(@results + ',', '') + convert(varchar(12),col)
from t
order by col
select @results as results
| RESULTS |
-----------
| 1,3,5,9 |
答案 1 :(得分:10)
select stuff(list,1,1,'')
from (
select ',' + cast(col1 as varchar(16)) as [text()]
from YourTable
for xml path('')
) as Sub(list)
答案 2 :(得分:4)
SQL Server 2017中有一种新方法:
SELECT STRING_AGG (column, ',') AS column FROM Table;
会为您产生1,3,5,9
答案 3 :(得分:1)
SELECT CAST(<COLUMN Name> AS VARCHAR(3)) + ','
FROM <TABLE Name>
FOR XML PATH('')
答案 4 :(得分:0)
ALTER PROCEDURE [dbo].[spConvertir_CampoACadena]( @nomb_tabla varchar(30),
@campo_tabla varchar(30),
@delimitador varchar(5),
@respuesta varchar(max) OUTPUT
)
AS
DECLARE @query varchar(1000),
@cadena varchar(500)
BEGIN
SET @query = 'SELECT @cadena = COALESCE(@cadena + '''+ @delimitador +''', '+ '''''' +') + '+ @campo_tabla + ' FROM '+@nomb_tabla
--select @query
EXEC(@query)
SET @respuesta = @cadena
END
答案 5 :(得分:0)
您可以使用以下方法:
select
STUFF(
(
select ', ' + CONVERT(varchar(10), ID) FROM @temp
where ID<50
group by ID for xml path('')
), 1, 2, '') as IDs
Declare @temp Table(
ID int
)
insert into @temp
(ID)
values
(1)
insert into @temp
(ID)
values
(3)
insert into @temp
(ID)
values
(5)
insert into @temp
(ID)
values
(9)
select
STUFF(
(
select ', ' + CONVERT(varchar(10), ID) FROM @temp
where ID<50
group by ID for xml path('')
), 1, 2, '') as IDs
答案 6 :(得分:0)
当前接受的答案不适用于多个分组。
当您需要对列行值的类别进行操作时,请尝试此操作。
假设我有以下数据:
+---------+-----------+
| column1 | column2 |
+---------+-----------+
| cat | Felon |
| cat | Purz |
| dog | Fido |
| dog | Beethoven |
| dog | Buddy |
| bird | Tweety |
+---------+-----------+
我希望这是我的输出:
+------+----------------------+
| type | names |
+------+----------------------+
| cat | Felon,Purz |
| dog | Fido,Beethoven,Buddy |
| bird | Tweety |
+------+----------------------+
(如果您遵循:
create table #column_to_list (column1 varchar(30), column2 varchar(30))
insert into #column_to_list
values
('cat','Felon'),
('cat','Purz'),
('dog','Fido'),
('dog','Beethoven'),
('dog','Buddy'),
('bird','Tweety')
)
现在–我不想讲所有语法,但是如您所见,这对我们来说是最初的技巧:
select ',' + cast(column2 as varchar(255)) as [text()]
from #column_to_list sub
where column1 = 'dog'
for xml path('')
--Using "as [text()]" here is specific to the “for XML” line after our where clause and we can’t give a name to our selection, hence the weird column_name
输出:
+------------------------------------------+
| XML_F52E2B61-18A1-11d1-B105-00805F49916B |
+------------------------------------------+
| ,Fido,Beethoven,Buddy |
+------------------------------------------+
您会看到它的局限性,因为它仅用于一个分组(其中column1 =“ dog”),并且在前面保留了一个逗号,此外它的名称也很奇怪。
因此,首先让我们使用'stuff'函数处理开头的逗号,并将其命名为stuff_list列:
select stuff([list],1,1,'') as stuff_list
from (select ',' + cast(column2 as varchar(255)) as [text()]
from #column_to_list sub
where column1 = 'dog'
for xml path('')
) sub_query([list])
--"sub_query([list])" just names our column as '[list]' so we can refer to it in the stuff function.
输出:
+----------------------+
| stuff_list |
+----------------------+
| Fido,Beethoven,Buddy |
+----------------------+
最后,将其粘贴到select语句中,注意对top_query别名的引用,该别名定义了我们想要的column1(在此处的第5行):
select top_query.column1,
(select stuff([list],1,1,'') as stuff_list
from (select ',' + cast(column2 as varchar(255)) as [text()]
from #column_to_list sub
where sub.column1 = top_query.column1
for xml path('')
) sub_query([list])
) as pet_list
from #column_to_list top_query
group by column1
order by column1
输出:
+---------+----------------------+
| column1 | pet_list |
+---------+----------------------+
| bird | Tweety |
| cat | Felon,Purz |
| dog | Fido,Beethoven,Buddy |
+---------+----------------------+
我们完成了。
您可以在此处了解更多信息:
答案 7 :(得分:0)
使用最简单的方法进行操作-
SELECT GROUP_CONCAT(Column) from table
答案 8 :(得分:0)
+------+----------------------+
| type | names |
+------+----------------------+
| cat | Felon |
| cat | Purz |
| dog | Fido |
| dog | Beethoven |
| dog | Buddy |
| bird | Tweety |
+------+----------------------+
select group_concat(name) from Pets
group by type
在这里,您可以轻松地在单个 SQL 中获得答案,并且通过在您的 SQL 中使用 group by,您可以根据该列值分离结果。您也可以使用自己的自定义分隔符来分割值
结果:
+------+----------------------+
| type | names |
+------+----------------------+
| cat | Felon,Purz |
| dog | Fido,Beethoven,Buddy |
| bird | Tweety |
+------+----------------------+
答案 9 :(得分:-1)
使用 LISTAGG 功能,
例如$('.modal-footer').on('click', '.edit', function() {
$.ajax({
method: 'POST',
url: '{{route('project.update', $project->id)}}',
data: {
'_method': 'PUT',
'_token': $('input[name=_token]').val(),
'id': $('#fid').val(),
'project_name': $("#pn").val(),
'date': $('#Da').val(),
'location': $('#Lo').val(),
'first_name': $('#FN').val(),
'last_name': $('#LN').val(),
'city_name': $('#CN').val(),
'email': $('#EM').val(),
'number': $('#NUM').val()
},
success: function(data){if(data.success == true){ // if true (1)
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 2000); }},