Case 2,
1 2 3 4 5
|
select regexp_replace('919047242526','^([[:digit:]]{2})([[:digit:]]{10})','+\1 \2') COL_VAL; COLA COL_VAL ------------ --------------- 919047255555 +91 9047255555
|
Case 3,
1 2 3 4 5
|
select regexp_replace('+++C','^([[:punct:]]{2})([[:punct:]]{1})(.*)$','\1\3') COL_VAL; COLA COL_VAL ---- ----- +++C ++C
|
Case 4,
1 2 3 4 5
|
select initcap(regexp_replace(regexp_substr(' satyaki.de@mail.com','[^@]+'),'(.*)(\.)(.*)','\1 \3')) COL_VAL; COLA COL_VAL -------------------------------- -------------------------------------------------- satyaki.de@mail.com Satyaki De
|
Case 5,
1 2 3 4 5
|
select regexp_replace('100011001','([[:digit:]]{3})([[:digit:]]{2})([[:digit:]]{4})','XXX-XX-\3') as COL_VAL; COLA COL_VAL ---------------- -------------------- 100011001 XXX-XX-1001
|
Case 6,
1 2 3 4 5
|
select regexp_replace('123456789','([[:digit:]]{3})([[:digit:]]{3})([[:digit:]]{3})','\3.\2.\1') as COL_VAL; COLA COL_VAL --------- --------------- 123456789 789.456.123
|
Case 7,
1 2 3 4 5
|
SELECT regexp_replace('satyaki9de0loves3to8work2on2sql0and2bi6tools1','[^0-9]+','',1,0,'i') AS DER_VAL;
COLA DER_VAL --------------------------------------------- ---------- satyaki1de0loves3to8work2on2sql0and2bi4tools1 1038220241
|
As you can see, all the characters have filtered out from the string & only numbers are kept here. These sorts of queries are very useful in lots of different business scenarios as well.
So, any extra space may not produce desired result. And, needs to pay attention into these small details.
And, I’ve tested all these queries in the following two versions –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
select * from dbcinfo; InfoKey InfoData -------- ------------------------ 1 VERSION 14.10.00.02 2 RELEASE 14.10.00.02 3 LANGUAGE SUPPORT MODE Standard select * from dbcinfo; InfoKey InfoData -------- ------------------------ 1 VERSION 14.10.01.05 2 RELEASE 14.10.01.04 3 LANGUAGE SUPPORT MODE Standard
|
Hope, this will give you much more clarity. 🙂
One more thing, I would like to clarify here – my intention is to describe more features about these regexp_(similar/substr/instr/replace) functions.
I’ve received one question whether these regexp functions available in TD 13 or not in Teradata forum while posting the same article over there.
And, here is my answer to that question –
Regarding version 13,
Let us check whether they have these regexp functions or not –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
select * from dbcinfo; InfoKey InfoData -------- ------------------------ 1 VERSION 13.00.00.15 2 RELEASE 13.00.00.15 3 LANGUAGE SUPPORT MODE Standard select * from dbcinfo; InfoKey InfoData -------- ------------------------ 1 VERSION 13.10.07.12 2 RELEASE 13.10.07.12 3 LANGUAGE SUPPORT MODE Standard
|
1 2 3 4 5 6 7 8 9
|
select regexp_replace('SatyakiDe','^(.*)([[:upper:]]{1,})(.*) $','\1 \2\3') AS COL_VAL; select regexp_replace('SatyakiDe','^(.*)([[:upper:]]{1,})(.*) $','\1 \2\3') AS COL_VAL; select regexp_replace('SatyakiDe','^(.*)([[:upper:]]{1,})(.*) $','\1 \2\3') AS COL_VAL; $ *** Failure 3706 Syntax error: expected something between '(' and the string 'S' keyword. Statement# 1, Info =35 *** Total elapsed time was 1 second.
|
Hope this will give adequate clarity to the answer of that above question.
Now, Lets see some other functionality.
REGEXP_SIMILAR has similar functionality like REGEXP_LIKE in Oracle.
Let’s see couple of such cases –
Lets prepare the table with some dummy data –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
SELECT * FROM dbc.dbcinfo; InfoKey InfoData -------- ----------------------- 1 VERSION 14.10.01.05 2 RELEASE 14.10.01.04 3 LANGUAGE SUPPORT MODE Standard CREATE MULTISET VOLATILE TABLE TEST_T1 ( COL1 VARCHAR(10) ) ON COMMIT PRESERVE ROWS; INSERT INTO TEST_T1 VALUES('456') ;INSERT INTO TEST_T1 VALUES('123x') ;INSERT INTO TEST_T1 VALUES('x123') ;INSERT INTO TEST_T1 VALUES('y') ;INSERT INTO TEST_T1 VALUES('+789') ;INSERT INTO TEST_T1 VALUES('-789') ;INSERT INTO TEST_T1 VALUES('159-') ;INSERT INTO TEST_T1 VALUES('-1-');
|
Lets check the data now –
1 2 3 4 5 6 7 8 9 10 11 12
|
SELECT * FROM TEST_T1; COL1 1 123x 2 456 3 x123 4 +789 5 -789 6 y 7 159- 8 -1-
|
Let’s look into the various scenarios now –
Case 1 (Returns Mixed Numbers, Signed Numbers & Non Numbers),
1 2 3 4 5 6 7 8 9 10 11 12 13
|
SELECT * FROM TEST_T1 WHERE REGEXP_SIMILAR(COL1,'^[0-9]+$','c')=0; COL1 ----- 1 123x 2 x123 3 +789 4 -789 5 y 6 159- 7 -1-
|
Case 2 (Returns Only Unsigned Positive Numbers),
1 2 3 4 5 6 7
|
SELECT * FROM TEST_T1 WHERE REGEXP_SIMILAR(COL1,'^[0-9]+$','c')=1; COL1 ----- 456
|
Case 3 (Returns All Numbers including Positive, Negative & unsigned),
1 2 3 4 5 6 7 8 9 10 11
|
SELECT * FROM TEST_T1 WHERE REGEXP_SIMILAR(COL1,'^[+-]?[0-9]+[+-]?$','c')=1; COL1 ----- 456 +789 -789 159- -1-
|
Case 4 (Returns Only Non Numbers i.e. Characters),
1 2 3 4 5 6 7
|
SELECT * FROM TEST_T1 WHERE REGEXP_SIMILAR(COL1,'[^0-9]+','c')=1;
COL1 ---- y
|
Hope this will give you some additional idea. 🙂
My objective is to provide basic information to my friends. So, that they can write better SQL in TD while migrating from other popular databases or new developer in TD can get a flavor of this powerful feature & exploit them in all the positive aspect & apply them properly. 😀
Really appreciate your time to read this post.
Regards.
Satyaki De.
You must be logged in to post a comment.