SQL如何根据符号截取字符串
1个回答
展开全部
刚好写过一个拆分字符串的,用动态SQL就可以实现:
declare
@T
table
(sortid
int,mychar
varchar(10))
declare
@i
int
declare
@str
varchar(20)
set
@str
=
'1,3,11,5,20'
set
@i
=
1
while
charindex(',',@str)
>
0
begin
insert
into
@T
values(@i,
substring(@str,1,charindex(',',@str)-1))
set
@str
=
substring(@str,charindex(',',@str)+1,len(@str))
set
@i
=
@i
+
1
end
insert
into
@T
values(@i,
@str)
select
*
from
@T
declare
@T
table
(sortid
int,mychar
varchar(10))
declare
@i
int
declare
@str
varchar(20)
set
@str
=
'1,3,11,5,20'
set
@i
=
1
while
charindex(',',@str)
>
0
begin
insert
into
@T
values(@i,
substring(@str,1,charindex(',',@str)-1))
set
@str
=
substring(@str,charindex(',',@str)+1,len(@str))
set
@i
=
@i
+
1
end
insert
into
@T
values(@i,
@str)
select
*
from
@T
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询