Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AsimKhaliq
Yak Posting Veteran
94 Posts |
Posted - 2003-12-06 : 08:08:15
|
| HiIam using thisSelect Substring('ABC_KHL_PKL_GGG',1,CHARINDEX('_','ABC_KHL_PKL')-1)And its giving me the result ABC which is okBut how i will get the get 2nd, 3rd and 4th part of above stringLike resultWill be KHLPKLGGGThanks in advance |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2003-12-06 : 08:11:45
|
If you always have the form <a_b_c_d> then you can have a look at the PARSENAME functionhttp://www.sqlteam.com/item.asp?ItemID=15044create table #object_piece ( n int)insert into #object_piece select 4union all select 3union all select 2union all select 1declare @string varchar(100); set @string = 'ABC_KHL_PKL_GGG'SELECT PARSENAME(REPLACE(@string,'_','.'),n) from #object_piece otherwise take a look at the following article:http://www.sqlteam.com/item.asp?ItemID=2652 |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-12-06 : 10:19:04
|
| create table #t (n varchar(800))insert into #t select 'A_B_QWER_GG_U_K'select distinctsubstring(n,charindex('_',n,num)+1,charindex('_',n,num+1)-charindex('_',n,num)-1)from (select '_'+n+'_end' n from #t) tcross join(select num=1 union select 2 union select 3 unionselect 4 union select 5 union select 6 union select 7 unionselect 8 union select 9 union select 10 union select 11 unionselect 12 union select 13 union select 14 union select 15 union select 16) zwhere charindex('_',n,num+1)-charindex('_',n,num)-1>0drop table #t |
 |
|
|
|
|
|
|
|