问题
I have a column named Cons in a table named Conses as Order::Resource(PPP32#BB300320LQ00J#AAAR05504)
I have second table which depends on first table.
i wants to get all data from the seconds table and i did it manually as below and result is ok
select * from so_db..item where id =('PPP32' )
and sub_id =('BB300320LQ00J')
and tem_id =('AAAR05504');
but i want to replace inside the parentheses with an other query which must get one part of the column mentioned above as follow:
id = first part of column before '#' character
sun_id = middle part of column between '#' character
tem_id = last part of column
select * from so_db..item where id =( select Cons from er_db..Conses where UsedBy_ = 'mmmf8c713f490f8133c00e16ffdea136add')
and sub_id =(select Cons from er_db..Conses where UsedBy_ = 'mmmf8c713f490f8133c00e16ffdea136add')
and tem_id =(select Cons from er_db..Conses where UsedBy_ = 'mmmf8c713f490f8133c00e16ffdea136add');
Does anybody know how to do this?
回答1:
You simply have to do string manipulation.
Use substring, charindex, char_length, right and left functions :)
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))
来源:https://stackoverflow.com/questions/12173470/how-to-get-part-of-the-column-value-in-sybase-where-this-column-situated-in-a-qu