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.
Related
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;
Query:
SELECT t3.e_id, t6.ei_id
FROM t3, t6
WHERE
(TO_CHAR(t6.EI_Q17, 'YYYYMMDD') BETWEEN 20160801 AND 20170731)
AND (t3.E_STATUS = 'W')
AND (t3.E_START < '1/8/2016')
AND (t3.E_END > '31/7/2017')
but I get an error: ORA-01843: not a valid month.
Any idea how this can be resolved?
Thanks,
Ar
Use standard date syntax and explicit joins:
SELECT t3.e_id, t6.ei_id
FROM t3 CROSS JOIN t6
WHERE t6.EI_Q17 BETWEEN DATE '2016-08-01' AND DATE '2017-07-31' AND
t3.E_STATUS = 'W' AND
t3.E_START < DATE '2016-08-01' AND
t3.E_END > DATE '2017-07-31';
If you use DATE and ISO 8601 standard date formats, then you don't have to worry about the localization settings for date constants. You also don't need to clutter up your queries with function calls.
Firstly you must follow Gordon suggestion to Use standard date syntax and explicit joins. Secondly from your posted query it looks like the column t6.EI_Q17 is varchar having date. In this case you need to cast it twice like below:
SELECT t3.e_id, t6.ei_id
FROM t3 CROSS JOIN t6
WHERE TO_CHAR (TO_DATE (t6.EI_Q17, 'YYYY-MM-DD'), 'YYYYMMDD') BETWEEN 20160801
AND 20170731
AND t3.E_STATUS = 'W'
AND t3.E_START < TO_DATE ('2016-08-01', 'YYYY-MM-DD')
AND t3.E_END > TO_DATE ('2017-07-31', 'YYYY-MM-DD')
Simple demo:
SQL> select dt
from
(select to_char(to_date('2017-06-01','YYYY-MM-DD'),'YYYYMMDD') dt from dual )
where dt between 20170601 and 20170603 ;
Output:
20170601
I have a column of dates that has 2 different formats of data: yyyy-mm-dd and dd/mm/yyyy.
Because of that, I have to indentify the format of the date so I can convert them in the right format. My SQL query is as follows:
select *
from teste
where
(CASE
WHEN data like '%-%' THEN
TO_DATE(data, 'yyyy-mm-dd') BETWEEN
TO_DATE(01/01/2000, 'dd/mm/yyyy') AND
TO_DATE(01/01/2016, 'dd/mm/yyyy')
ELSE
TO_DATE(data, 'dd/mm/yyyy') BETWEEN
TO_DATE(01/01/2000, 'dd/mm/yyyy') AND
TO_DATE(01/01/2016, 'dd/mm/yyyy')
END)
Running this, I get the error:
00905. 00000 - "missing keyword"
*Cause:
*Action:
Erro na linha: 6 Coluna: 35.
It seems that there is a problem in the between clause. Could anyone help me with this?
CASE expression returns you some value. And you should compare this value with something. You can't put whole expression inside CASE.
E.g.
select *
from teste
where
CASE WHEN data like '%-%' THEN
TO_DATE(data, 'yyyy-mm-dd')
ELSE
TO_DATE(data, 'dd/mm/yyyy')
END
BETWEEN TO_DATE(01/01/2000, 'dd/mm/yyyy') AND
TO_DATE(01/01/2016, 'dd/mm/yyyy')
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
select TO_CHAR(to_date(sysdate, 'DD-MON-YYYY'), 'DAY') FROM DUAL;
When I run this query the output was : SUNDAY. But we know today is Tuesday(1-1-2013).
And
then changed the query as
select TO_CHAR(to_date('01-JAN-2013', 'DD-MON-YYYY'), 'DAY') FROM DUAL;
answer was :TUESDAY.
then Changed query as
select TO_CHAR(to_date(sysdate+1, 'DD-MON-YYYY'), 'DAY') FROM DUAL;
answer is :MONDAY.
When I using the sysdate why it is show SUNDAY as output?
I am new in oracle db. Please help me.
use this:
select TO_CHAR(sysdate, 'DAY') FROM DUAL;
you are using this :
to_date(sysdate, 'DD-MON-YYYY')
which is giving you date=1/1/0013 which is sunday
Please refer the documentation for sysdate here. Sysdate is already a date data type.
Your example query is inappropriate as to_date function takes first parameter as String not date.
Try the simple query below:
select TO_CHAR(sysdate, 'DAY') FROM DUAL;
This should return TUESDAY as output.
To_date is used to convert a strin to date. As sysdate is already a date, one must not add add to_date.