Convert Numeric format dates to dates in Oracle - oracle

This is how my Date column looks like
RPT_DT (Data Type is Number)
20180131
20180130
20180129
I wanna extract month out of these dates(either Month or mm), and I tried below
select extract(month from to_date(Rpt_dt))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)
I am getting the error "Not a valid month"

if there is not any particular reason to have a double conversion I would suggest you to handle the problem with this simple query:
select substr(to_char(RPT_DT),5,2)from THE_TABLE
this query should be more performant since it make only one conversion. in your sample you transform:
a number to a date
then a date to a char
the char again in date
finally you extract the month
let me know if it help
r.

try this,
SELECT EXTRACT(MONTH FROM TO_DATE(rpt_dt, 'YYYYMMDD'))
FROM TABLE_NAME;
and I believe you need to modify your query as you did not put the format 'MM/DD/YYYY',
select extract(month from to_date(Rpt_dt, 'MM/DD/YYYY'))
from
(
select distinct to_char(to_date(RPT_DT,'yyyymmdd'),'mm/dd/yyyy') Rpt_dt
from TABLE_NAME
)

This back-and-forth conversion is useless. Try this
select
extract(month from to_date(RPT_DT,'yyyymmdd'))
from TABLE_NAME;

Related

encountered the symbol FROM when expecting one of the following pl sql

I am trying to extract year from datefield but when I use extract (year from
datefield) I get this error
Encountered the symbol FROM when expecting one of the following pl sql
cursor o1 is
select substr(tarifa,1,2), count(*)
from pol p, uvod u, doppov d
where extract(year FROM datum_dop) = EXTRACT(YEAR FROM sysdate)
and izdavanje >='1-jul-13'
and p.orgjed = u.sorgz (+)
and DATUM_PREKIDA is not null
and p.polica=d.polica and d.pov_dopl='P'
and d.status='F'
and cisti_ao(p.polica)!=0
group by substr(tarifa,1,2);
Where did I made mistake ?
Ah, this is Forms, probably 6i.
Its engine doesn't know extract function. Change that line to
where to_char(datum_dop, 'yyyy') = to_char(sysdate, 'yyyy')
This would, though, make index on datum_dop column (if it exists) unusable and force Oracle to convert dates to strings, so you'd rather try with
where datum_dop >= trunc(sysdate, 'yyyy')
and datum_dop < add_months(trunc(sysdate, 'yyyy'), 12)
Other than that:
count(*) should have an alias (if you plan to use it), e.g. count(*) as broj_tarifa
if izdavanje is date, don't compare it to a string ('1-jul-13') but date, e.g. izdavanje >= to_date('01.07.2013', 'dd.mm.yyyy')
use table aliases for all columns

Delete from Oracle table rows older than 1 month

In Oracle I have a table with a DB_FLD_4 defined as VARCHAR2(20 BYTE) but actually holds DATE information with looks like this:
select DB_FLD_4 from DM_SUPDES_DISTRIB order by DB_FLD_4 desc;
4/9/2017
4/7/2017
4/6/2017
Kind of m/d/yyyy format.
I would like to create SP that would delete every day rows from the table older than 1 month.
My sysdate looks like this:
select sysdate from dual;
SYSDATE
24-APR-17 04.41:00
Please help to manipulate formats to make this possible.
Many thanks !!!
Use to_date() on your column, then trunc the sysdate to "round" to the day
delete
from MyTable
where to_date(MyColumn, 'MM/DD/YYYY') < add_months(trunc(sysdate), -1)

Extract year and month from timestamp(6)

I have database, where i have inserted timestamps(6) in this format :
18-AUG-14 02.49.27.000000000 PM .
I want to extract it into this: 2014-08
Its called ISO 8601
You'll need to use the to_char function to extract the year-month from timestamp.
select to_char(timestamp, 'yyyy-mm') from your_table
I've done this in this way -
select extract(year from timestmp) || '-' || extract(month from timestmp) from texmp1;
Hope this helps.
Here is the table structure:
create table texmp1
(
timestmp timestamp
);

Change the data format

I use a script in oracle which select at a moment a date :
CURSOR SAURON IS select TOUTDOUX_ID, TYPE_OF_ACTION, USER_ID, PROFILE_NAME, START_DATE, END_DATE, PLATFORM, COMMENTS, PERM_FLAG, ACTIVE_FLAG from uam.tout_doux
But the format (25-JUL-2013) is not the one I expected (2013/07/25).
How can I select the date with the right format ?
Use the Oracle TO_CHAR function with date-time format elements. In your case you want the format string YYYY/MM/DD:
CURSOR SAURON IS
select TOUTDOUX_ID, TYPE_OF_ACTION, USER_ID, PROFILE_NAME,
TO_CHAR(START_DATE, 'YYYY/MM/DD') AS SDate,
TO_CHAR(END_DATE, 'YYYY/MM/DD') AS EDate,
PLATFORM, COMMENTS, PERM_FLAG, ACTIVE_FLAG
from uam.tout_doux

Oracle: handle different date format

Assume we have a VARCHAR2 column called MyDate
Assume it contains the following values:
13/02/2001
13-gen-2001
I have to convert this field to a DATE value. But TO_DATE fails either on some values or on the others.
TO_DATE(MyDate, 'DD/MM/YYYY')
How can I convert it?
SELECT CASE
WHEN REGEXP_LIKE(mydate, '\d{2}/\d{2}/\d{4}') THEN
TO_DATE(mydate, 'DD/MM/YYYY')
WHEN REGEXP_LIKE(mydate, '\d{2}-[a-z]{3}-\d{4}') THEN
TO_DATE(mydate, 'DD-MON-YYYY')
END
FROM mytable
you can write like this
CASE WHEN instr(labour_contract_expiry, '-') > 0 THEN
to_date(LABOUR_CONTRACT_EXPIRY,'DD-MON-RRRR')
ELSE
to_date(LABOUR_CONTRACT_EXPIRY,'DD/MM/RRRR')
END,
At least you can do something like
CASE WHEN REGEXP_LIKE(YOUR_COLUMN,'\d{2}/\d{2}/\d{4}' THEN TO_DATE(MyDate, 'DD/MM/YYYY')
WHEN REGEXP_LIKE(YOUR_COLUMN,'\d{2}-\D{,3}-\d{4}' THEN TO_DATE(MyDate, 'DD-MON-YYYY')
WHEN ...
etc.

Resources