SQL Server拆分字符串的3常用種方法
作者:佚名
以下的文章主要描述的是SQL Server拆分字符串的3常用種方法,以及對其實際操作中要用到的實際應用代碼的具體描述。
以下的文章主要是介紹的是SQL Server拆分字符串的3常用種方法,我前兩天在相關網(wǎng)站看見SQL Server拆分字符串的3常用種方法的資料,覺得挺好,就拿出來供大家分享,望會給大家?guī)硪恍椭诖朔矫妗?/p>
- use tempdb
- use tempdb
- go
測試數(shù)據(jù)
- declare @s varchar(1000)
- set @s='ak47,mp5,1,23'
要求輸出結果
- S
- ak47
- mp5
- 1
- 23
SQL Server拆分字符串的3種方法對比:
SQL Server拆分字符串1.[樸實]動態(tài)Exec方法:
- declare @s1 varchar(1000)
- set @s1=right(replace(','+@s,',',''' as S union select '''),len(replace(','+@s,',',''' as S union select '''))-12)+''''
- exec(@s1)
SQL Server拆分字符串2.[變通]表交叉方法:
- select replace(reverse((left(s,charindex(',',s)))),',','') as S from(
- select r,reverse(left(@s,r))+',' as s
- from(
- select (select count(*) from sysobjects where name<=t.name ) as r
- from sysobjects t
- )a where r<=len(@s)
- and left(@s+',',r+1) like '%,'
- )t order by r
SQL Server拆分字符串3.[高級]XML方法:
- DECLARE @idoc int;
- DECLARE @doc xml;
- set @doc=cast('<Root><item><S>'+replace(@s,',','</S></item><item><S>')+'</S></item></Root>' as xml)
- EXEC sp_xml_preparedocument @Idoc OUTPUT, @doc
- SELECT * FROM OPENXML (@Idoc, '/Root/item',2)
- WITH (
- [S] varchar(10)
- )
以上的相關內(nèi)容就是對SQL Server拆分字符串的三種方法的介紹,望你能有所收獲。
【編輯推薦】
責任編輯:佚名
來源:
theserverside