sql查询里 怎么拆分字符串(按“/”拆分)
答案:3 悬赏:10 手机版
解决时间 2021-04-01 15:32
- 提问者网友:别再叽里呱啦
- 2021-04-01 11:00
sql查询里 怎么拆分字符串(按“/”拆分)
最佳答案
- 五星知识达人网友:行路难
- 2021-04-01 11:54
先建立一个自定义函数,之个函数非常有用,建议收入自已的数据库
CREATE FUNCTION mysplit--将以某分隔符分段的字串,按指定的顺序号提取子串:
(@str nvarchar(2000),--源字串
@sn int, --提取序号
@Deli varchar(1) --分隔符
)
RETURNS varchar(100)
AS
BEGIN
declare @first int,@last int,@result varchar(1000),@sn0 int
select @sn0=0,@first=0,@LAST=1,@str=@str+REPLICATE(@DELI,1)
while @sn0!=@sn
begin
select @sn0=@sn0+1,@first=@LAST,@last=charindex(@DELI,@str,@LAST)+1
end
if @last-@first-1<0
set @result=''
else
SET @RESULT=SUBSTRINg(@str,@FIRST,@LAST-@FIRST-1)
RETURN ( @RESULT )
END查询方法:
DECLARE @A VARCHAr(100),@B VARCHAr(100),@C VARCHAr(100)
SELECT
@A=DBO.MYSPLIT('A|B|C',1,'|') ,
@B=DBO.MYSPLIT('A|B|C',2,'|') ,
@C=DBO.MYSPLIT('A|B|C',3,'|')
SELECT @A,@B,@C
CREATE FUNCTION mysplit--将以某分隔符分段的字串,按指定的顺序号提取子串:
(@str nvarchar(2000),--源字串
@sn int, --提取序号
@Deli varchar(1) --分隔符
)
RETURNS varchar(100)
AS
BEGIN
declare @first int,@last int,@result varchar(1000),@sn0 int
select @sn0=0,@first=0,@LAST=1,@str=@str+REPLICATE(@DELI,1)
while @sn0!=@sn
begin
select @sn0=@sn0+1,@first=@LAST,@last=charindex(@DELI,@str,@LAST)+1
end
if @last-@first-1<0
set @result=''
else
SET @RESULT=SUBSTRINg(@str,@FIRST,@LAST-@FIRST-1)
RETURN ( @RESULT )
END查询方法:
DECLARE @A VARCHAr(100),@B VARCHAr(100),@C VARCHAr(100)
SELECT
@A=DBO.MYSPLIT('A|B|C',1,'|') ,
@B=DBO.MYSPLIT('A|B|C',2,'|') ,
@C=DBO.MYSPLIT('A|B|C',3,'|')
SELECT @A,@B,@C
全部回答
- 1楼网友:何以畏孤独
- 2021-04-01 14:12
itjob上有视频看
- 2楼网友:煞尾
- 2021-04-01 13:11
自定义split函数
CREATE FUNCTION [dbo].[split]
(@str nvarchar(4000),@code varchar(10),@no int )
RETURNS varchar(200)
AS
BEGIN
declare @intLen int
declare @count int
declare @indexb int
declare @indexe int
set @intLen=len(@code)
set @count=0
set @indexb=1
if @no=0
if charindex(@code,@str,@indexb)<>0
return left(@str,charindex(@code,@str,@indexb)-1)
else
return @str
while charindex(@code,@str,@indexb)<>0
begin
set @count=@count+1
if @count=@no
break
set @indexb=@intLen+charindex(@code,@str,@indexb)
end
if @count=@no
begin
set @indexe=@intLen+charindex(@code,@str,@indexb)
if charindex(@code,@str,@indexe)<>0
return substring(@str,charindex(@code,@str,@indexb)+len(@code),charindex(@code,@str,@indexe)-charindex(@code,@str,@indexb)-len(@code))
else
return right(@str,len(@str)-charindex(@code,@str,@indexb)-len(@code)+1)
end
return ''
END
CREATE FUNCTION [dbo].[split]
(@str nvarchar(4000),@code varchar(10),@no int )
RETURNS varchar(200)
AS
BEGIN
declare @intLen int
declare @count int
declare @indexb int
declare @indexe int
set @intLen=len(@code)
set @count=0
set @indexb=1
if @no=0
if charindex(@code,@str,@indexb)<>0
return left(@str,charindex(@code,@str,@indexb)-1)
else
return @str
while charindex(@code,@str,@indexb)<>0
begin
set @count=@count+1
if @count=@no
break
set @indexb=@intLen+charindex(@code,@str,@indexb)
end
if @count=@no
begin
set @indexe=@intLen+charindex(@code,@str,@indexb)
if charindex(@code,@str,@indexe)<>0
return substring(@str,charindex(@code,@str,@indexb)+len(@code),charindex(@code,@str,@indexe)-charindex(@code,@str,@indexb)-len(@code))
else
return right(@str,len(@str)-charindex(@code,@str,@indexb)-len(@code)+1)
end
return ''
END
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯