日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

04. 字符串合并与拆分写法小结

發布時間:2025/5/22 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 04. 字符串合并与拆分写法小结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
04. 字符串合并與拆分寫法小結 原文:04. 字符串合并與拆分寫法小結

一. 字符合并

if OBJECT_ID('ConcatStr') is not null drop table ConcatStr GO create table ConcatStr ( ID int, Code varchar(10) ) GO insert into ConcatStr select 1,'XXX' union all select 1,'YYY' union all select 2,'PPP' union all select 2,'QQQ'

?要得到這樣的結果:

IDCode
1XXX,YYY
2PPP,QQQ

1. 用游標

declare @t table(ID int, Code varchar(1000)) declare @id int declare c cursor for select distinct ID from ConcatStr open c fetch next from c into @id while @@fetch_status=0 begin declare @str varchar(max) set @str = '' select @str = @str + ',' + Code from ConcatStr where ID = @id insert into @t(ID, Code) select @id,stuff(@str,1,1,'') fetch next from c into @id end close c deallocate c select * from @t

?2. 用自定義函數

跟游標的方法類似,只是把逐個取的動作封裝到函數里去了。
(1) 函數方法1

if OBJECT_ID('f_concat_str') is not null drop function f_concat_str GO create function f_concat_str(@id int) returns nvarchar(4000) as begin declare @s nvarchar(4000) set @s='' select @s = @s+',' + Code from ConcatStr where ID = @id return (stuff(@s,1,1,'')) --return (right(@s,len(@s)-1)) End

?(2) 函數方法2,就是把函數1再簡化

if OBJECT_ID('f_concat_str') is not null drop function f_concat_str GO create function f_concat_str(@id int) returns nvarchar(4000) as begin declare @s nvarchar(4000) --set @s='' --select @s = case when @s = '' then Code else @s + ',' + Code end --from ConcatStr where ID = @id select @s = isnull(@s + ',','') + Code from ConcatStr where ID = @id return @s end

?調用函數1或者函數2

--select ID,dbo.f_concat_str(ID) as Code --from ConcatStr --group by ID Select distinct ID, Code = dbo.f_concat_str(ID) from ConcatStr

?3. 利用靜態的行列轉換寫法

給分組里的每行構造一個編號,行列轉換后把列連接起來,編號多少個,取決于每個分組COUNT(1)的值。

SELECT ID, MAX(CASE WHEN num = 1 THEN Code ELSE '' END) + MAX(CASE WHEN num = 2 THEN ',' + Code ELSE '' END) AS Code FROM (SELECT ID, Code,(SELECT COUNT(*) FROM dbo.ConcatStr AS t2 WHERE t2.ID = t1.ID AND t2.Code <= t1.Code) AS num FROM dbo.ConcatStr AS t1) AS t GROUP BY ID;

?4. 用FOR XML子句

(1) FOR XML AUTO
SQL Server 2000就有這個子句,不過OUTER APPLY是SQL Server 2005的語法。通常這種寫法效率上不會比用函數快。

SELECT * FROM(SELECT DISTINCT ID FROM ConcatStr)A OUTER APPLY(SELECT Code= STUFF(REPLACE(REPLACE(( SELECT Code FROM ConcatStr N WHERE ID = A.ID FOR XML AUTO), '<N Code="', ','), '"/>', ''), 1, 1, ''))N

?(2) FOR XML PATH

SQL Server 2005的新語法。

SELECT ID, STUFF((SELECT ',' + Code FROM dbo.ConcatStr AS t2 WHERE t2.ID = t1.ID ORDER BY ID FOR XML PATH('')), 1, 1, '') AS Code FROM dbo.ConcatStr AS t1 GROUP BY ID;

?

二. 字符拆分

if not object_id('SplitStr') is null drop table SplitStr Go create table SplitStr ( Col1 int, Col2 nvarchar(10) ) insert SplitStr select 1,N'a,b,c' union all select 2,N'd,e' union all select 3,N'f' Go

?要得到這樣的結果:

Col1Code
1a
1b
1c
2d
2e
3f

1. 使用數字輔助表

if object_id('Tempdb..#Num') is not null drop table #Num GO select top 100 ID = Identity(int,1,1) into #Num --也可用ROW_NUMBER()來生成 from syscolumns a,syscolumns b GO Select a.Col1,Col2=substring(a.Col2,b.ID,charindex(',',a.Col2+',',b.ID)-b.ID) from SplitStr a,#Num b where charindex(',',','+a.Col2,b.ID)=b.ID --也可用substring(','+a.COl2,b.ID,1)=','

?2. 使用CTE

with t(Col1, p1, p2) as ( select Col1, charindex(',',','+col2), charindex(',',Col2+',') + 1 from SplitStr union all select s.Col1, t.p2, charindex(',', s.Col2+',', t.p2) + 1 from SplitStr s join t on s.Col1 = t.Col1 where charindex(',', s.Col2+',', t.p2) > 0 ) --select * from t select s.Col1, Col2 = substring(s.Col2+',', t.p1, t.p2-t.p1-1) from SplitStr s join t on s.Col1 = t.Col1 order by s.Col1 option (maxrecursion 0)

?3. 使用XML

SELECT A.Col1, B.Code FROM(SELECT Col1, Code = CONVERT(XML,'<root><v>' + REPLACE(Col2, ',', '</v><v>') + '</v></root>') FROM SplitStr) A OUTER APPLY(SELECT Code = N.v.value('.', 'varchar(100)') FROM A.Code.nodes('/root/v') N(v)) B

??

posted on 2014-09-06 11:21 NET未來之路 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/lonelyxmas/p/3959244.html

總結

以上是生活随笔為你收集整理的04. 字符串合并与拆分写法小结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。