Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Substring in SQL

I have to find substring as follows.

Data as below


aaaa.bbb.ccc.dddd.eee.fff.ggg 

qq.eeddde.rrr.t.hh.jj.jj.hh.hh 

ee.r.t.y.u.i.ii.

I want output as-


bbb

eeeddde

r

challenge I am facing is all have (.) as separator so sub-string is tough to work.

SELECT SUBSTRING(string,CHARINDEX('.',string)+1,
       (((LEN(string))-CHARINDEX('.', REVERSE(string)))-CHARINDEX('.',string))) AS Result 
  FROM [table] 

bbb

eeeddde

r

looking substring between first and secound (.)

then it might be between second and third (.)

like image 364
Andrew Avatar asked Dec 23 '25 00:12

Andrew


1 Answers

Here is one method:

select left(v.str1, charindex('.', v.str1 + '.') - 1)
from t cross apply
     (values (stuff(t.string, 1, charindex('.', t.string + '.'), '')
     ) v(str1)
like image 171
Gordon Linoff Avatar answered Dec 24 '25 19:12

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!