Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

Search for first substring in string with condition - forum topic by PeterSchwennesen

$
0
0

Any help on string search. No problem if recursive. I do not know how to do the following.
I want to find the first position of “b” in the string where “b” is not between 2 “a”’s. The select statement I have made returns the value 6 for row nr (Seq=1). I want it to return the value 17, the first b outside the two “a”. Is this possible?
CREATE MULTISET VOLATILE TABLE T1 (Seq INTEGER, Str VARCHAR(100)) ON COMMIT PRESERVE ROWS;
INSERT INTO T1 VALUES (1,'a----b----a-----b----b------');
INSERT INTO T1 VALUES (2,'b----b----b------');
INSERT INTO T1 VALUES (3,'---b--a--b--a--b------');
INSERT INTO T1 VALUES (4,'--b----b----b------');
 
SELECT Seq,
       Str,
       INDEX(Str,'b') AS idx,
       CASE
         WHEN Str LIKE '%b%' AND NOT Str LIKE '%a%b%a%' THEN INDEX(Str,'b')
         ELSE INDEX(Str,'b')
       END
FROM T1;
DROP TABLE T1;
Peter Schwennesen

Forums: 

Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>