如何在 SQL 服务器中连接 unicode 字符串?
How to concatenate unicode strings in SQL Server?
我有一个独特的情况,我需要连接基于 2 个组的行。另一个技巧是串联必须能够显示项目符号并添加换行符。这就是我的企业所要求的。我还应该添加最终目的地将是 Excel。我已经使用带有项目符号和换行符的数据测试了通过 SSIS 导出到 Excel,这篇文章确实有效。
如何前往:
pk orgnl_pk type text
1 1 one • Line One
2 1 one • Line Two
3 1 one • Line Three
4 1 two • Line One
7 3 one • Line One
8 3 two • Line One
9 3 two • Line Two
收件人:
orgnl_pk type text
1 one • Line One
• Line Two
• Line Three
1 two • Line One
3 one • Line One
3 two • Line One
• Line Two
正如 Lamak 指出的那样,这最好留给表示层,但如果您现在必须在 sql 中执行...那么这将使用 stuff()
with select ... for xml path ('')
method of string concatenation。
select
orgnl_pk
, [type]
, [text]=stuff(
(
select char(10) +i.[text]
from t as i
where i.orgnl_pk = t.orgnl_pk
and i.[type]=t.[type]
order by i.pk
for xml path (''), type).value('.','nvarchar(max)')
,1,0,'')
from t
group by orgnl_pk, [type]
rextester 演示:http://rextester.com/GPFIMO37322
returns:
+----------+------+--------------+
| orgnl_pk | type | text |
+----------+------+--------------+
| 1 | one | • Line One |
| | | • Line Two |
| | | • Line Three |
| 1 | two | • Line One |
| 3 | one | • Line One |
| 3 | two | • Line One |
| | | • Line Two |
+----------+------+--------------+
字符 (0x0007) 更新:
select
orgnl_pk
, [type]
, [text]=replace(stuff(
(
select char(10)+replace(i.[text],nchar(0x0007),'$BEL$')
from t as i
where i.orgnl_pk = t.orgnl_pk
and i.[type]=t.[type]
order by i.pk
for xml path (''), type).value('.','nvarchar(max)')
,1,1,''),'$BEL$',nchar(0x0007))
from t
group by orgnl_pk, [type]
我有一个独特的情况,我需要连接基于 2 个组的行。另一个技巧是串联必须能够显示项目符号并添加换行符。这就是我的企业所要求的。我还应该添加最终目的地将是 Excel。我已经使用带有项目符号和换行符的数据测试了通过 SSIS 导出到 Excel,这篇文章确实有效。
如何前往:
pk orgnl_pk type text
1 1 one • Line One
2 1 one • Line Two
3 1 one • Line Three
4 1 two • Line One
7 3 one • Line One
8 3 two • Line One
9 3 two • Line Two
收件人:
orgnl_pk type text
1 one • Line One
• Line Two
• Line Three
1 two • Line One
3 one • Line One
3 two • Line One
• Line Two
正如 Lamak 指出的那样,这最好留给表示层,但如果您现在必须在 sql 中执行...那么这将使用 stuff()
with select ... for xml path ('')
method of string concatenation。
select
orgnl_pk
, [type]
, [text]=stuff(
(
select char(10) +i.[text]
from t as i
where i.orgnl_pk = t.orgnl_pk
and i.[type]=t.[type]
order by i.pk
for xml path (''), type).value('.','nvarchar(max)')
,1,0,'')
from t
group by orgnl_pk, [type]
rextester 演示:http://rextester.com/GPFIMO37322
returns:
+----------+------+--------------+
| orgnl_pk | type | text |
+----------+------+--------------+
| 1 | one | • Line One |
| | | • Line Two |
| | | • Line Three |
| 1 | two | • Line One |
| 3 | one | • Line One |
| 3 | two | • Line One |
| | | • Line Two |
+----------+------+--------------+
字符 (0x0007) 更新:
select
orgnl_pk
, [type]
, [text]=replace(stuff(
(
select char(10)+replace(i.[text],nchar(0x0007),'$BEL$')
from t as i
where i.orgnl_pk = t.orgnl_pk
and i.[type]=t.[type]
order by i.pk
for xml path (''), type).value('.','nvarchar(max)')
,1,1,''),'$BEL$',nchar(0x0007))
from t
group by orgnl_pk, [type]