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

Oracle "REPLACE" function equivalant function in Teradata - response (8) by varma.geet

$
0
0

A recursive implementation to replace multiple occurences of a search string with a replacement string in a column -
For 13.0/13.1; if you have Teradata 14.0 use OREPLACE.
We will try to replace all pipes in a Unix command with a string of our choice.
Example:
Steps to simulate: Create a table to hold the unix command. Insert the unix command. Now we are all set up. Run the recursive sql to output the changed command.
 
Create table–
 
CREATE SET TABLE test.test_table ,NO FALLBACK ,
     NO BEFORE JOURNAL,
     NO AFTER JOURNAL,
     CHECKSUM = DEFAULT,
     DEFAULT MERGEBLOCKRATIO
     (
      column1 INTEGER,
      column2 VARCHAR(100) CHARACTER SET LATIN NOT CASESPECIFIC FORMAT 'X(50)')
PRIMARY INDEX ( column1 );
 
Insert a test record – Notice the |’s -
 
INSERT INTO test.test_table
(column1, column2)
VALUES               
(1, 'wc -l example.txt |  sed -e ''s/^ *//g'' -e ''s/ *$//g'' | awk ''{PRINT $1}''');
 
This command counts the lines in an example.txt text file, removes the formatting and prints only the number of lines. We need to replace all pipes (2) with 'xxx'; the first one is not a pipe, it is an l (L).
 
Remove the pipes and replace with ‘xxx’!!!!!
 
With recursive recur(seq, column2) AS
(
select 1, column2 from test_table
 
union all
 
select seq+1, SUBSTRING(recur.column2 FROM 1 FOR POSITION('|' IN recur.column2)-1 ) || 'xxx' || SUBSTRING(recur.column2 FROM POSITION('|' INrecur.column2)+1 FOR CHARACTER_LENGTH(recur.column2)-1)
from recur, test_table
where  POSITION('|' IN recur.column2) > 0
)
select top 1 seq, column2
from recur
order by seq desc;
 
Since we have 2 pipes, it loops twice [3 db round trips in the recursive part]. Teradata 13.1 ONLY supports UNION ALL set operator in recursive queries. 
The example above works for 1 row at a time but can be extended/called to support multiple rows.


Viewing all articles
Browse latest Browse all 14773

Trending Articles



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