SQL里类似SPLIT的分割字符串函数
来源:互联网 发布:知乎首页不显示话题 编辑:程序博客网 时间:2024/05/29 19:49
方法一:
use studySET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: cxw-- Create date: 2006/11/28-- Description: 用递归实现字符串分割/*--函数具体用法:--最常用的调用:select * from dbo.RecurrentSplit('AA@BBB@CCC@','@',0,0)
select * from dbo.RecurrentSplit('AA@@@BBB@@@CCC@@@','@@@',null,null)--默认
--具体细节用法:--分割字符不显示,分割方法是按前面的分割字符串,只显示分割后的字符串select * from dbo.RecurrentSplit('AA@@@BBB@@@CCC@@@','@@@',0,1)--分割字符显示,并是按前面的来进行分割,分割字符放在被分割字符串的前面select * from dbo.RecurrentSplit('@@@AA@@@BBB@@@CCC@@@','@@@',1,1)--分割字符不显示,并是按后面的来进行分割,只显示分割后的字符串select * from dbo.RecurrentSplit('AA@@@BBB@@@CCC@@@','@@@',0,0)--分割字符显示,并是按后面的来进行分割,分割字符放在被分割字符串的后面select * from dbo.RecurrentSplit('AA@@@BBB@@@CCC@@@','@@@',1,0)*/-- =============================================Create FUNCTION DBO.RecurrentSplit(@nvStr nvarchar(2000) --需要分割字符串,@vSeparte varchar(50) --分割字符串,@iIsHaveSeparte int --是否显示字符串,@iIsBefore int --是否是后面的分割符(分割字符分割的顺序))RETURNS @Split table (IndexNo int default(0) --流水号,SplitName nvarchar(1000) --分割后字符串)ASBEGIN if(charindex(@vSeparte,@nvStr)<=0) --处理在整个字符串里都没有要分割,也就是字符串本身 begin insert into @Split(SplitName) values(@nvStr) return end declare @iSeparteLen int ,@iEndHave int --最后几个字符串是否是分割字符 ,@iStartHave int --前面几个字符串是否是分割字符 select @iSeparteLen=len(@vSeparte) ,@iStartHave=0 ,@iEndHave=0 ,@iIsHaveSeparte=case when @iIsHaveSeparte is null --默认值 then 0 else @iIsHaveSeparte end ,@iIsBefore=case when @iIsBefore is null --默认值 then 0 else @iIsBefore end if(@iIsBefore=1) --只有在处理前面字符串分割时才用 begin if(left(@nvStr,@iSeparteLen)<>@vSeparte)--处理前面几个分割字符一定是分割字符,不是就加 begin select @nvStr=@vSeparte+@nvStr ,@iStartHave=1 end end if(right(@nvStr,@iSeparteLen)<>@vSeparte)--处理最后几个分割字符一定是分割字符,不是就加 begin select @nvStr=@nvStr+@vSeparte ,@iEndHave=1 end; --分号一定不能少,因为用了with,是用公用表达式,在其前面一定要加; with CharCET(CharStr,StrLen,IndexNo) as ( select substring(@nvStr,0 ,case when @iIsBefore=0 then charindex(@vSeparte,@nvStr)+@iSeparteLen else charindex(@vSeparte,@nvStr,charindex(@vSeparte,@nvStr)+@iSeparteLen) end ) CharStr ,case when @iIsBefore=0 then charindex(@vSeparte,@nvStr) else charindex(@vSeparte,@nvStr,charindex(@vSeparte,@nvStr)+@iSeparteLen) end StrLen ,0 IndexNo --第一次初始化 union all select substring(@nvStr ,case when @iIsBefore=0 then StrLen+@iSeparteLen else StrLen end ,charindex(@vSeparte,@nvStr,StrLen+@iSeparteLen)-StrLen) CharStr ,charindex(@vSeparte,@nvStr,StrLen+@iSeparteLen) StrLen ,IndexNo+1 IndexNo --进行递归分割字符串 from CharCET where StrLen<len(@nvStr)-@iSeparteLen --处理递归结束语句,也就是不要让其生成无限弟归下去 ) insert into @Split(IndexNo,SplitName) select IndexNo,case when @iIsHaveSeparte=0 then replace(CharStr,@vSeparte,'') else CharStr end CharStr from CharCET if(@iIsHaveSeparte=1) --是否显示分割字符串 begin update @Split --处理前面的分割字符串 set SplitName=case when @iStartHave=1 then replace(SplitName,@vSeparte,'') else SplitName end where IndexNo = 0
update @Split --处理后面的分割字符串 set SplitName=case when @iEndHave=1 then replace(SplitName,@vSeparte,'') else SplitName end where IndexNo = (select Max(IndexNo) from @Split) end RETURN ENDGO
方法二:
declare @s varchar(8000),@sql nvarchar(4000)set @s='1,12,1212,4545'set @sql='insert into t(col) select '+replace(@s,',',' col union all select ')exec(@sql)测试drop table #tabledeclare @s varchar(8000),@sql nvarchar(4000)set @s='1,12,1212,4545,454'create table #table (col int)set @sql='insert into #table(col) select '+replace(@s,',',' col union all select ')exec(@sql)select * from #table
SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- SQL里类似SPLIT的分割字符串函数
- MySQL里实现类似SPLIT的分割字符串的函数
- MySQL里实现类似SPLIT的分割字符串的函数
- MySQL里实现类似SPLIT的分割字符串的函数
- mysql里的字符串类似split函数
- 黑马程序员--SQL Server 中实现类似Split的字符串分割函数
- SQL Server里用Split函数分割字符串
- SQL分割字符串 类似 C# Split
- SQL 字符串分割函数 split
- 2008年7月12日在广州举行软件质量管理的沙龙
- 每个.Net开发人员应该下载的十种必备工具 收藏
- 查询数组--传统方式、匿名方法和lambda表达式比较 2
- 需要掌握的八个CSS布局技巧
- Web网站的设计、管理与维护
- SQL里类似SPLIT的分割字符串函数
- JSP转向另一地址
- 开始学习
- SQL分割字符串函数
- 页面自动刷新代码
- C#通过注册表列出所有串口的实例
- 删除数据库中所有表的数据
- 间隔状态栏跑马灯
- commons fileupload 文件上传应用