Need to remove only character which are at the end - oracle

Need to remove only characters at the end.
2014JP34343DD
2013GH43422
Output:
2014JP34343
2013GH43422
Tried regexp fuctions and even simple substr and instr function but not able to remove it.

regexp_replace seems to be a simple option:
SQL> with test (col) as
2 (select '2014JP34343DD' from dual union all
3 select '2013GH43422' from dual
4 )
5 select col,
6 regexp_replace(col, '[[:alpha:]]+$') result
7 from test;
COL RESULT
------------- -------------
2014JP34343DD 2014JP34343
2013GH43422 2013GH43422
SQL>

Related

formatting a number to a display string

Converting a number to a string for example 20000000.00 like this 20 000 000.00 with gap. Please help me
One option is to use to_char function with nls_numeric_characters set to a dot (decimal point) and space (group separator):
SQL> with test (col) as
2 (select 20000000.00 from dual union all
3 select 12345.324 from dual)
4 select to_char(col, '999G999G999G990D00', 'nls_numeric_characters=''. ''') result
5 from test;
RESULT
-------------------
20 000 000.00
12 345.32
SQL>

Need to extract data from string by regexp_substr

I have a ["1101124","1101123","123456"], I need to get the end result as rows for the numbers which are in the bracket.
How can I achieve this by using regular expression in Oracle.
Don't use regular expression to try to parse JSON data; use a proper JSON parser:
SELECT value
FROM JSON_TABLE(
'["1101124","1101123","123456"]',
'$[*]'
COLUMNS(
value VARCHAR2(20) PATH '$'
)
)
Outputs:
VALUE
1101124
1101123
123456
db<>fiddle here
If ["1101124","1101123","123456"] is a string:
SQL> WITH DATA AS
2 ( SELECT '["1101124","1101123","123456"]' str FROM dual
3 )
4 SELECT trim(regexp_substr(str, '[0-9]+', 1, LEVEL)) str
5 FROM DATA
6 CONNECT BY regexp_substr(str , '[0-9]+', 1, LEVEL) IS NOT NULL
7 /
STR
----------------------------------------
1101124
1101123
123456
3 rows selected.
SQL>

Replace spaces after a dot in the string

I want to write a query in Oracle to remove any spaces after any dot in the string.
example :
select 'My name is Pramod. I am writing a query. Today is AUG 16TH.' from dual;
output requested is :
My name is Pramod.I am writing a query.Today is AUG 16TH.
A simple replace, based on your simple example:
SQL> with test (col) as
2 (select 'My name is Pramod. I am writing a query' from dual)
3 select replace(col, '. ', '.') result
4 from test;
RESULT
--------------------------------------
My name is Pramod.I am writing a query
SQL>
For not that simple examples:
SQL> with test (col) as
2 (select 'My name is Pramod. I am writing a query' from dual union all
3 select 'Right. As #Mathguy said. It won''t work.always' from dual
4 )
5 select regexp_replace(col, '\. +', '.') result
6 from test;
RESULT
--------------------------------------------------------------------------------
My name is Pramod.I am writing a query
Right.As #Mathguy said.It won't work.always
SQL>
Try this:
select regexp_replace('My name is Pramod. I am writing a query. Today is AUG 16TH.','\.(\s)*','.')
from dual
Sql fiddle Demo
Cheers!!
this works too.
select regexp_replace( 'Right. As #Mathguy said. It won''t work.always' ,'(\.)([[:space:]]*)', '.' ) from dual;

How to select second split of column data from oracle database

I want to select the data from a Oracle table, whereas the table columns contains the data as , [ex : key,value] separated values; so here I want to select the second split i.e, value
table column data as below :
column_data
++++++++++++++
asper,worse
tincher,good
golder
null -- null values need to eliminate while selection
www,ewe
from the above data, desired output like below:
column_data
+++++++++++++
worse
good
golder
ewe
Please help me with the query
According to data you provided, here are two options:
result1: regular expressions one (get the 2nd word if it exists; otherwise, get the 1st one)
result2: SUBSTR + INSTR combination
SQL> with test (col) as
2 (select 'asper,worse' from dual union all
3 select 'tincher,good' from dual union all
4 select 'golder' from dual union all
5 select null from dual union all
6 select 'www,ewe' from dual
7 )
8 select col,
9 nvl(regexp_substr(col, '\w+', 1, 2), regexp_substr(col, '\w+', 1,1 )) result1,
10 --
11 nvl(substr(col, instr(col, ',') + 1), col) result2
12 from test
13 where col is not null;
COL RESULT1 RESULT2
------------ -------------------- --------------------
asper,worse worse worse
tincher,good good good
golder golder golder
www,ewe ewe ewe
SQL>

remove a varchar2 string from the middle of table data values

Data in the file_name field of the generation table should be an assigned number, then _01, _02, or _03, etc. and then .pdf (example 82617_01.pdf).
Somewhere, the program is putting a state name and sometimes a date/time stamp, between the assigned number and the 01, 02, etc. (82617_ALABAMA_01.pdf or 19998_MAINE_07-31-2010_11-05-59_AM.pdf or 5485325_OREGON_01.pdf for example).
We would like to develop a SQL statement to find the bad file names and fix them. In theory it seems rather simple to find file names that include a varchar2 data type and remove it, but putting the statement together is beyond me.
Any help or suggestions appreciated.
Something like:
UPDATE GENERATION
SET FILE_NAME (?)
WHERE FILE_NAME (?...LIKE '%STRING%');?
You can find the problem rows like this:
select *
from Files
where length(FILE_NAME) - length(replace(FILE_NAME, '_', '')) > 1
You can fix them like this:
update Files
set FILE_NAME = SUBSTR(FILE_NAME, 1, instr(FILE_NAME, '_') -1) ||
SUBSTR(FILE_NAME, instr(FILE_NAME, '_', 1, 2))
where length(FILE_NAME) - length(replace(FILE_NAME, '_', '')) > 1
SQL Fiddle Example
You can also use Regexp_replace function:
SQL> with t1(col) as(
2 select '82617_mm_01.pdf' from dual union all
3 select '456546_khkjh_89kjh_67_01.pdf' from dual union all
4 select '19998_MAINE_07-31-2010_11-05-59_AM.pdf' from dual union all
5 select '5485325_OREGON_01.pdf' from dual
6 )
7 select col
8 , regexp_replace(col, '^([0-9]+)_(.*)_(\d{2}\.pdf)$', '\1_\3') res
9 from t1;
COL RES
-------------------------------------- -----------------------------------------
82617_mm_01.pdf 82617_01.pdf
456546_khkjh_89kjh_67_01.pdf 456546_01.pdf
19998_MAINE_07-31-2010_11-05-59_AM.pdf 19998_MAINE_07-31-2010_11-05-59_AM.pdf
5485325_OREGON_01.pdf 5485325_01.pdf
To display good or bad data regexp_like function will come in handy:
SQL> with t1(col) as(
2 select '826170_01.pdf' from dual union all
3 select '456546_01.pdf' from dual union all
4 select '19998_MAINE_07-31-2010_11-05-59_AM.pdf' from dual union all
5 select '5485325_OREGON_01.pdf' from dual
6 )
7 select col bad_data
8 from t1
9 where not regexp_like(col, '^[0-9]+_\d{2}\.pdf$');
BAD_DATA
--------------------------------------
19998_MAINE_07-31-2010_11-05-59_AM.pdf
5485325_OREGON_01.pdf
SQL> with t1(col) as(
2 select '826170_01.pdf' from dual union all
3 select '456546_01.pdf' from dual union all
4 select '19998_MAINE_07-31-2010_11-05-59_AM.pdf' from dual union all
5 select '5485325_OREGON_01.pdf' from dual
6 )
7 select col good_data
8 from t1
9 where regexp_like(col, '^[0-9]+_\d{2}\.pdf$');
GOOD_DATA
--------------------------------------
826170_01.pdf
456546_01.pdf
To that end your update statement might look like this:
update your_table
set col = regexp_replace(col, '^([0-9]+)_(.*)_(\d{2}\.pdf)$', '\1_\3');
--where clause if needed

Resources