如何从sybase中的字符串中提取子字符串?

时间:2012-08-29 09:10:56

标签: sql sybase-ase

通过查询我得到这个字符串"Order::Resource(PPP32#BB300320LQ00J#AAAR05504)"。 我想在#字符之前,之间和之后提取字符串,如下所示:

id = PPP32

sub_id = BB300320LQ00J

sup_id = AAAR05504

有谁知道怎么做?

2 个答案:

答案 0 :(得分:3)

declare @start int, @end int, @secondstring varchar(100)

select @start = charindex('#',@string)
select @secondstring = substring(@string, @start+1, len(@string))

select @end = charindex('#',@secondstring)

select substring(@string,1, @start-1),
       substring(@string,@start+1,@end-1), 
       substring(@string, @start+@end+1, len(@string)-@end)

答案 1 :(得分:3)

这是正确的解决方案:

declare @string varchar(50)
select @string = 'Order::Resource(PPP32#BB300320LQ00J#AAAR05504)'
declare @start int, @end int, @secondstring varchar(100)

select @start = charindex('#',@string)
select @secondstring = substring(@string, @start+1, len(@string))

select @end = charindex('#',@secondstring)

select substring(@string,charindex('(', @string)+1, @start-1-charindex('(', @string)),
       substring(@string,@start+1,@end-1), 
       substring(@string, @start+@end+1, len(@string)-(@start+@end+1))