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;
Related
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>
What is the sql code to print 'Query' if the data in field = 'Q'?
Welcome to SO!
An example out of the box:
select decode(dummy, 'X', 'Y') from dual;
For your scenario, something like:
select decode(mycol, 'Q', 'Query') mycol from mytable;
Best of luck!
Preferred option is to use CASE because of readability; although, as #Bjarte suggested, DECODE can also be used (which I what I do, especially for simple cases). Also, tables have columns, not fields.
Anyway, CASE:
SQL> with test (field) as
2 -- sample data; you already have that and don't type it
3 (select 'A' from dual union all
4 select 'Q' from dual union all
5 select 'B' from dual
6 )
7 -- query you need
8 select field,
9 case when field = 'A' then 'Answer'
10 when field = 'Q' then 'Query'
11 else 'Unknown'
12 end as result
13 from test;
F RESULT
- -------
A Answer
Q Query
B Unknown
SQL>
I have fully qualified class names like below.
'com.test.Person', 'com.test.fruit.Apple', 'com.test.color.Red'
I need to get the simple class names Person,Apple,Red as result from these strings using oracle query.
You can use substr. And instr for find last '.'
with test (col)
as (select 'com.test.Person' from DUAL
union all
select 'com.test.fruit.Apple' from DUAL
union all
select 'com.test.color.Red' from DUAL)
select col, SUBSTR (col, INSTR (col, '.', -1) + 1) class
from test
This?
SQL> with test (col) as
2 (select 'com.test.Person' from dual union all
3 select 'com.test.fruit.Apple' from dual union all
4 select 'com.test.color.Red' from dual
5 )
6 select col,
7 regexp_substr(col, '\w+$') result
8 from test;
COL RESULT
-------------------- --------------------
com.test.Person Person
com.test.fruit.Apple Apple
com.test.color.Red Red
SQL>
I want to concat to the output based on condition. Here is my query:
select 'hey',
case id =1 then 'Mary' else 'Tom' end
from names;'
I want to print 'hey tom' or 'hey mary' based on id ... any help ??
It is concatenation you need; in Oracle, double pipe || represents that operator:
SQL> with names (id) as
2 (select 1 from dual union all
3 select 2 from dual union all
4 select 3 from dual
5 )
6 select id,
7 'hey ' || case when id = 1 then 'Mary'
8 else 'Tom'
9 end result
10 from names;
ID RESULT
---------- ----------
1 hey Mary
2 hey Tom
3 hey Tom
SQL>
You can run something like this query:
SELECT CONCAT('hey ', FIRST_NAME) from names where ID = '1';
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