Reverse String using Regexp Functions

Hi,

 

Today, we’ll be checking one new area where we can implement regular expression to achieve the same without involving any kind of Macro, Stored-Proc.

 

Many occasion we may have to parse various kind of strings. Assume that, we need to parse the string in reverse order. Until TD 14.0, you don’t have any easy method to implement the same. Off course, this new method also has some limits. It can only able to reverse couple of characters only. I have checked with 9 characters.  It supports that.

 

So, if you have specific string lengths, then you may also try this solution if you are using TD 14.0. That will be handy.

 

Let’s check the Query & Output –

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
SELECT SEQ_NO,
                 SRC_STR,
                 regexp_replace(SRC_STR,'([[:alnum:]]{1,1})([[:alnum:]]{0,1})([[:alnum:]]{0,1})([[:alnum:]]{0,1})([[:alnum:]]{0,1})([[:alnum:]]{0,1})([[:alnum:]]{0,1})([[:alnum:]]{0,1})([[:alnum:]]{0,1})','\9\8\7\6\5\4\3\2\1') AS REV_SRC_STR
FROM WM_CONCAT_TAB
ORDER BY 1;


SEQ_NO	SRC_STR	 REV_SRC_STR
-----   -------  --------------
1	BIRESWAR RAWSERIB
1	TRIDIB	 BIDIRT
1	SUJAY	 YAJUS
1	ANUPAM	 MAPUNA
1	RAM	 MAR
2	PAPU	 UPAP
2	SAYAN	 NAYAS
2	TUNKAI	 IAKNUT
2	BABU	 UBAB
3	ATBIS	 SIBTA
3	SAPMUNDA ADNUMPAS
3	IK	 KI

 

Hope, this will give you another way – to implement the same logic. 🙂

Leave a Reply