Perl non greedy expression - perl-module

I want to match following SQL statement:
insert into EMP ( DATE ) values ( TO_DATE('05/13/2013 00:00','mm/dd/yyyy hh24:mi') );
with following expression:
TO_DATE\((.*)\)?
But its matching following:
TO_DATE('05/13/2013 00:00','mm/dd/yyyy hh24:mi') );
I want to only match following:
TO_DATE('05/13/2013 00:00','mm/dd/yyyy hh24:mi')
What should the RE be?

Try TO_DATE\((.*?)\)?
Adding ? to * makes it non-greedy.
UPDATE
This should work: /TO_DATE(\(.*?\))/, this will include the first set of parentheses inside the capture group.

Related

How to use replace function in Oracle to remove a string?

In my table, I have data like PAT5DSA-(ALRP), LAR6DAOP-(RAH) etc..But I want to remove the strings like -(xxxx) or -(xxx) which can be any alphabets inside braces. Tried using the below:
select replace(:code,'-(Aa-Zz)',null) from employee;
But this didn't work..Can anyone please help?
We can do a regex replacement using REGEXP_REPLACE:
SELECT REGEXP_REPLACE('PAT5DSA-(ALRP)', '-\(.*?\)', '')
FROM dual;
PAT5DSA
The plain replace() doesn't understand patterns. You could use a regular expression replace, e.g.:
-- CTE for sample data
with cte (code) as (
select 'PAT5DSA-(ALRP)' from dual
union all
select 'LAR6DAOP-(RAH)' from dual
)
select code, regexp_replace(code, '-\(.*?\)$') as result
from cte;
CODE RESULT
-------------- --------------
PAT5DSA-(ALRP) PAT5DSA
LAR6DAOP-(RAH) LAR6DAOP
This will remove anything inside a pair of parentheses which is preceded by a dash, at the end of the original string. If the parentheses to be removed could be anywhere in the string then remove the $.
Use INSTR and SUBSTR:
WITH cteVals AS (SELECT 'PAT5DSA-(ALRP)' AS COL_VAL FROM DUAL UNION ALL
SELECT 'LAR6DAOP-(RAH)' AS COL_VAL FROM DUAL)
SELECT SUBSTR(COL_VAL, 1, INSTR(COL_VAL, '-')-1)
FROM cteVals;
Best of luck.

How to replace string using Regexp_Replace in oracle

I want to replace this:
"STORES/KOL#10/8/36#1718.00#4165570.00#119539388#PT3624496#9902001#04266#6721#PT3624496-11608091-1-55-STORES/KOL"
with this:
"STORES/KOL#10#8#36#1718.00#4165570.00#119539388#PT3624496#9902001#04266#6721#PT3624496-11608091-1-55-STORES/KOL"
basically this is conditional based replace I want to replace / with #
like STORES/KOL string should be STORES/KOL
but 10/8/36 string should be 10#8#36
This will replace the 2nd and 3rd / character with a #:
Oracle Setup:
CREATE TABLE test_data ( value ) AS
SELECT '"STORES/KOL#10/8/36#1718.00#4165570.00#119539388#PT3624496#9902001#04266#6721#PT3624496-11608091-1-55-STORES/KOL"'
FROM DUAL;
Query:
SELECT REGEXP_REPLACE(
value,
'^(.*?/.*?)/(.*?)/(.*)$',
'\1#\2#\3'
) AS replacement
FROM test_data
Output:
| REPLACEMENT |
| :---------------------------------------------------------------------------------------------------------------- |
| "STORES/KOL#10#8#36#1718.00#4165570.00#119539388#PT3624496#9902001#04266#6721#PT3624496-11608091-1-55-STORES/KOL" |
db<>fiddle here
Here is one option using REGEXP_REPLACE. We can try targeting the following regex pattern:
#(\d+)/(\d+)/(\d+)#
Then replace using the three capture groups, replacing the path separators with pound signs.
WITH yourTable AS (
SELECT 'STORES/KOL#10/8/36#1718.00#4165570.00#119539388#PT3624496#9902001#04266#6721#PT3624496-11608091-1-55-STORES/KOL' AS input FROM dual
)
SELECT
input,
REGEXP_REPLACE(input, '#(\d+)/(\d+)/(\d+)#', '#\1#\2#\3#') AS output
FROM yourTable;
Demo
Whether or not this regex replacement is specific enough/accurate for the rest of your data depends on that data, which you never showed us.
with s as (select '"STORES/KOL#10/8/36#1718.00#4165570.00#119539388#PT3624496#9902001#04266#6721#PT3624496-11608091-1-55-STORES/KOL"' str from dual)
select
replace(replace(str, '/', '#'), 'STORES#KOL', 'STORES/KOL') result_str_1,
regexp_replace(str, '(\d)/', '\1#') result_str_2
from s;

How substr works on dates

I am trying to understand a query in my application where it uses substr function.
I have gone through the documentation for substr, which looks simple and clear.
Now below is my query without using substr:
select last_day(to_date(to_char(add_months(TO_DATE('2004/10/25', 'yyyy/mm/dd'),1),'YYYY')||'0201','YYYYMMDD')) from dual;
This gives me result as 2/29/2004. The above query just returns last day of Feb in simple words.
Now I am using substr as below:
select substr(last_day(to_date(to_char(add_months(TO_DATE('2004/10/25', 'yyyy/mm/dd'),1),'YYYY')||'0201','YYYYMMDD')),5,1) from dual;
So here the start value is 5 & length is 1, so I am expecting output as 2 looking at 2/29/2004. but the actual output is E, I am not clear from where this E is coming as result.
You cannot use SUBSTR() on DATE values. SUBSTR() works only on strings!
When you run SUBSTR({DATE_VALUE}, ...) then Oracle actually does following:
SELECT
SUBSTR(
TO_CHAR(
{DATE_VALUE}, (SELECT VALUE FROM nls_session_parameters WHERE parameter = 'NLS_DATE_FORMAT')
), ...
)
FROM dual;
What is the purpose of this query? Do you like to find out whether input year is a leap-year?
Try this instead -
select substr(to_char(last_day(to_date(to_char(add_months(TO_DATE('2004/10/25', 'yyyy/mm/dd'),1),'YYYY')||'0201','YYYYMMDD')),'dd/mm/yyyy'),5,1)
from dual;

Replace multiple substrings with one expression in Oracle SQL

I would like to delete multiple substrings from one column. I tried the replace function with the following code:
select replace('testetstestetststst', 'test'||'et'||'s', '')
from dual;
My expected result is ttt, but I get tstst.
In R it works with:
gsub("test|et|s", "", "testetstestetststst")
How can I replace many different substrings with nothing ('') in a column in clob format in Oracle SQL?
You need the REGEXP version of REPLACE:
select regexp_replace('testetstestetststst', 'test|et|s', '')
from dual;
In your code, you are concatenating strings, instead of using an OR operator; that is, your code is equivalent to
select replace('testetstestetststst', 'testets', '')
from dual;
Rather than using regular expressions, you can nest multiple REPLACE functions:
SELECT REPLACE(
REPLACE(
REPLACE(
'testetstestetststst',
'test'
),
'et'
),
's'
)
FROM DUAL;
We can directly use decode function.
select decode(job,'clerk','1','manager','2','salesman','3',4) from emp;
This will replace clerk with 1,manager with 2,salesman with 3 and other values with 4.

Oracle appending string to a select statement

I have a column in a oracle table Lic_num char(7 byte)
SELECT column1, 'ABC' + Lic_num
FROM TABLE One
I wanted ABC appended to all the rows that are returned with lic_num
appended to it.
I tried tha above query and it is not working.
In Oracle it's:
SELECT column1, 'ABC' || Lic_num
FROM TABLE_ONE
This would be the way of doing it.
SELECT column1, 'ABC' || Lic_num FROM TABLE_ONE;
SELECT CONCAT(CONCAT(column1, 'ABC'), Lic_num) FROM TABLE_ONE;
If you need you can rename the concatenated Column name using AS keyword so it would be meaningful in terms of reporting.
Below info is included to help someone looking at concatenation in detail.
There are two ways to concatenate Strings in Oracle SQL. Either using CONCAT function or || operator.
CONCAT function allows you to concatenate two strings together
SELECT CONCAT( string1, string2 ) FROM dual;
Since CONCAT function will only allow you to concatenate two values together. If you want to concatenate more values than two, you can nest multiple CONCAT function calls.
SELECT CONCAT(CONCAT('A', 'B'),'C') FROM dual;
An alternative to using the CONCAT function would be to use the || operator
SELECT 'My Name' || 'My Age' FROM dual;

Resources