MRR*_*MRR 1 sql t-sql sql-server string-aggregation stuff
有没有其他方法可以连接 SQL 列并用逗号分隔。我使用以下逻辑进行串联。列 (col1,col2,col3) 可以具有空值。
select
stuff(
left(concat(col1,',',col2,',',col3),
len(concat(col1,',',col2,',',col3)) -
patindex('%[^,]%',reverse(concat(col1,',',col2,',',col3)))+1
)
,1,
patindex('%[^,]%',concat(col1,',',col2,',',col3))-1,''
)
from mytable
Run Code Online (Sandbox Code Playgroud)
在较新版本的 SQL Server 中,您可以使用concat_ws():
select concat_ws(',', col1, col2, col3)
Run Code Online (Sandbox Code Playgroud)
在早期版本中,有多种方法。一个非常简单的方法是:
select stuff( concat(',' + col1, ',' + col2, ',' + col3), 1, 1, '')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12109 次 |
| 最近记录: |