~~~~~~~~~~~~~~还是字符串截取问题,100分求教~~~~~~~~~~~

wendi 2006-06-09 04:25:55
825hw-03-4/10/12
825hw-03-7/10/4
825hw-03-11/2/14

如何用一条语句将 825hw-03-4/10/12 中的 4,10,12 和 825hw-03-7/10/4 中的7,10,4 和 825hw-03-11/2/14中的 11,2,14分别截取出来,谢谢!

...全文
209 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
itblog 2006-06-09
  • 打赏
  • 举报
回复

declare @a varchar(2000)
select @a='825hw-03-4/10/12
825hw-03-7/10/4
825hw-03-11/2/14'
print @a
select @a=stuff(replace(replace(replace(@a,'825hw-03-',','),'
',''),'/',','),1,1,'')
print @a
LouisXIV 2006-06-09
  • 打赏
  • 举报
回复

declare @a varchar(100)
set @a='825hw-03-4/10/12'
set @a=substring(@a,len('825hw-03-')+1,len(@a)-len('825hw-03-'))+'/'

declare @b table
(
idx int identity(1,1),
Val int
)

declare @c int
while len(@a)>0
begin
insert into @b select left(@a,charindex('/',@a)-1)
set @a=substring(@a,len(left(@a,charindex('/',@a)))+1,10)
end
select * from @b
fcuandy 2006-06-09
  • 打赏
  • 举报
回复
--生成测试数据
declare @tb table(c char(30))
insert @tb
select '825hw-03-4/10/12'
union
select '825hw-03-7/10/4'
union
select '825hw-03-11/2/14'

select * from @tb

DECLARE @str VARCHAR(8000)
SET @str=''
SELECT @str=@str + RTRIM(SUBSTRING(c,10,8)) + '/' FROM @tb
select @str=replace(left(@str,len(@str)-1),'/',',')
select @str

显示
---------------
11,2,14,4,10,12,7,10,4
如果要将数字一个个显示:

先自定义split函数
--------------------------
Create Function Split(@Sql varchar(8000),@Splits varchar(10))
returns @temp Table (a varchar(100))
As
Begin
Declare @i Int
Set @Sql = RTrim(LTrim(@Sql))
Set @i = CharIndex(@Splits,@Sql)
While @i >= 1
Begin
Insert @temp Values(Left(@Sql,@i-1))
Set @Sql = SubString(@Sql,@i+1,Len(@Sql)-@i)
Set @i = CharIndex(@Splits,@Sql)
End

If @Sql <> ''
Insert @temp Values (@Sql)
Return
End


然后
SELECT * FROM dbo.Split(@str,',')
paoluo 2006-06-09
  • 打赏
  • 举报
回复
Declare @S Varchar(1000)
Set @S='825hw-03-4/10/12'
--Set @S='825hw-03-7/10/4'
--Set @S='825hw-03-11/2/14'
Select Substring(@S,Len(@S)-CharIndex('-',Reverse(@S))+2,CharIndex('/',@S)-Len(@S)+CharIndex('-',Reverse(@S))-2),
SubString(@S,CharIndex('/',@S)+1,CharIndex('/',@S,CharIndex('/',@S)+1)-CharIndex('/',@S)-1),
SubString(@S,CharIndex('/',@S,CharIndex('/',@S)+1)+1,Len(@S))
--Result
/*
4 10 12
7 10 4
11 2 14
*/
itblog 2006-06-09
  • 打赏
  • 举报
回复
declare @t table(a varchar(2000))
insert @t select '825hw-03-4/10/12'
insert @t select '825hw-03-7/10/4'
insert @t select '825hw-03-11/2/14'

select a=replace(right(a,charindex('-',REVERSE (a))-1),'/',',') from @t
itblog 2006-06-09
  • 打赏
  • 举报
回复
declare @a varchar(1000)
select @a='825hw-03-11/2/14'
select @a=replace(right(@a,charindex('-',REVERSE (@a))-1),'/',',')
print @a
LouisXIV 2006-06-09
  • 打赏
  • 举报
回复
一条语句??

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧