sqlserver字串多行合併為一行

2022-07-18 06:21:10 字數 1629 閱讀 8291

--

建立測試表

ifobject_id('

test

') is

notnull

drop

table

test

create

table

dbo.test(

id intidentity(1,1) not

null

, name

varchar(50) null

, subject

varchar(50) null

, source

decimal(18,2) null)go

--插入測試資料

insert

into

test (name,subject,source)

select'張三

','語文

',60

union

allselect'張三

','英語

',90

union

allselect'李四

','語文

',70

union

allselect'李四

','數學

',80

union

allselect'王五

','數學

',75

union

allselect'王五

','英語

',80

goselect

*from

test

--1 通過 select 累加

declare

@sql_col

varchar(8000

)select

@sql_col

=isnull(@sql_col+'

,','') +

quotename(subject) from

test

group

bysubject

select

@sql_col

--2 通過 for xml path('') 合併字串記錄

select

stuff

( (

select'#

'+subject

from

test

where name ='王五

'for xml path(''

) ),

1,1,'')

--3 分組合併字串記錄

select

name,

subject =(

stuff

( (

select'#

'+subject

from

test

where name =

a.name

for xml path(''

) ),

1,1,''

) )

from

test a

group

by name

多行字串 模板字串

多行字串 下面是普通字串的寫法 普通字串 var l abcd console.log l 編譯結果 如何讓讓乙個字串獨佔多行呢?就需要用到es6 裡的多行字串 多行字串 var i ab cd console.log i 編譯結果 再說說拼接字串,一般情況我們是如何拼接字串的呢?看下面 正常拼接字...

python多行字串

python中如何處理長 格式化問題,如何提高格式化輸出的長字串的可讀性?當我們需要格式化輸出乙個很長的字串的時候,都寫在一行顯得很難看,而且可讀性也很差 當我們使用鏈式的語法寫 的時候常常會超出螢幕的顯示區域。很多語言都有這方面的支援,但是python出現之後,無人能出其右,與其媲美。下面我們看看...

SQL字串分割多行

原型資料 需求 按逗號或分號將字段it description值分割為多行,填充下拉列表 sql實現指令碼 select b.vv from select convert xml,replace replace it description,as xmlval from tb invoicetype...