Oracle sql date timestamp filter [duplicate] - oracle

This question already has answers here:
Encountering SQL Error: ORA-01843: not a valid month
(5 answers)
ORACLE - ORA-01843: not a valid month
(2 answers)
Closed 4 years ago.
My Oracle table has 1,000 observations the following 2 variables: (Both fields are varchar)
ID datecreate
ABC 24/12/2016 05:32:07
I would like to filter the dates. I tried the following but to no avail...
select *
from table
where datecreate >= to_date('01/02/2018 00:00:00', 'dd/mm/yyyy hh24:mi:ss') and
datecreate <= to_date('28/02/2018 23:59:00', 'dd/mm/yyyy hh24:mi:ss')
I keep getting the following error...
ORA-01843: not a valid month
01843. 00000 - "not a valid month"
*Cause:
*Action:
Did I miss something obvious?
Thanks

It has been emphasized several times that in databases, DATE and TIME should not be stored as strings. Yet, there are several questions raised everyday, arising solely due to this poor practice.
Note that in Oracle, datetimes can either be stored as DATE type or TIMESTAMP( with or without TIMEZONE ). The DATE type contains the time component as well.
TO_DATE function comes to our rescue in cases where date strings have to be dealt with for conversion. While using dates, it is a good practice to use standard DATE literals in 'YYYY-MM-DD' format which most DBMSes understand. ( eg:- DATE '2018-02-28' ).
Also, you need not use '28/02/2018 23:59:00' or TRUNC function for such boundary cases, when you can use DATE literals. So, your query could be rewritten as
SELECT *
FROM yourtable
WHERE TO_DATE(datecreate, 'dd/mm/yyyy hh24:mi:ss') >= DATE '2018-02-01'
AND TO_DATE(datecreate, 'dd/mm/yyyy hh24:mi:ss') < DATE '2018-02-28' + 1
or < DATE '2020-02-29' if you know that it's a leap year.
If you really want to consider till 23:59 minutes, then TIMESTAMP literals could be used with TO_TIMESTAMP function.
.. AND TO_TIMESTAMP(datecreate, 'dd/mm/yyyy hh24:mi:ss') <
TIMESTAMP '2018-02-28 23:59:00'

The default NLS_DATE_FORMAT currently is DD-MON-RR. Thus your datecreate column defined as string will be implicitly converts to that format. The value 12 is not the same as DEC, thus the error. Either you use the right format or convert it to date similar below.
select *
from tbl
where to_date(datecreate, 'dd/mm/yyyy hh24:mi:ss') >=
to_date('01/02/2016 00:00:00', 'dd/mm/yyyy hh24:mi:ss')
and to_date(datecreate, 'dd/mm/yyyy hh24:mi:ss') <=
to_date('28/02/2018 23:59:00', 'dd/mm/yyyy hh24:mi:ss')

Related

Oracle query not giving result for current_date

What is the query in Oracle to fetch the data for current_date
the column end_date is like the following
end_date
27-10-16 03:35:00.000000000 PM
23-11-16 11:15:00.000000000 AM
02-11-16 03:00:00.000000000 PM
08-11-16 09:00:00.000000000 AM
Like I am running the following query as
Select * from table1
where end_date < TO_DATE('2017-04-11 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
it is running successfully, but when i replace the query with the current date ... it is not giving the results
Select * from table1
where end_date < TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS')
could someone tell me what is the cause the second query is not giving results.
CURRENT_DATE returns date. There is no need to use TO_DATE. The below query should be enough.
Select * from table1
where end_date < current_date;
If you run the below query you'll understand what went wrong for you. Year becomes 0011.
SELECT TO_DATE(current_date, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;
Please note that CURRENT_DATE returns the current date in the session time zone. SYSDATE returns the current date and time set for the operating system on which the database resides. This means that CURRENT_DATE and SYSDATE can return different results. You can have a look at this
The query worked like this :
Select * from table1
where trunc(end_date) < trunc(sysdate)
Trunc is used to compare the both dates and it fetch the results.
CURRENT_DATE is already a DATE value. You can format the output using to_char if you want.
end_date < CURRENT_DATE should do the job. Or you can set the nls parameter accordingly for a better readability.
If you are comparing only date, without timestamp, you can go with trunc()

Alter a datetime in Oracle and set time to 9 am

i have 28-APR-2016 10:05:07 date as parameter in stored procedure. This may be the current time also as string date.
i need to set the time to 9 am to check the shift start timing.
SELECT TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS') FROM dual;
I am new to oracle. Help is appreciated.
If you want the date with 9:00 a.m., then you can do:
SELECT TRUNC(TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS')) + 9/24.0
FROM dual;
You can also use:
SELECT TRUNC(TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS')) + INTERVAL '9' HOUR
FROM dual;
I'm just old-fashioned so I tend to use the first method.

Get date of the previous day in Oracle

I need to bring the day immediately preceding date in Oracle using a truncate but not how. He was using the following line but bring me some records for the current day of execution and should not be. Neceisto only the previous day; investigation found the truncate with dates in Oracle but not how to use it.
and fnxs.FECHA_INGRESO BETWEEN (TO_CHAR (SYSDATE-1, 'DD-MON-YY')) AND (TO_CHAR (SYSDATE, 'DD-MON-YY'));
I appreciate your help
Using BETWEEN with dates in Oracle is generally a bad idea. I see it all the time, and most of the time people get it wrong (like in the accepted answer above). Even when they fully understand that the two dates are included, they still make logical errors because they forget about timestamps.
The OP is asking for yesterday dates. The following sql shows that today falls within "BETWEEN TRUNC( SYSDATE ) - 1 AND TRUNC( SYSDATE )"
with adate as (
select trunc(sysdate) today from dual
) select today from adate where today between trunc(sysdate) -1
and trunc(sysdate);
16-Apr-15 00:00:00
[returns the record for today]
I find it easier to be correct with dates when you're more explicit about the end points:
SELECT * from your_table
WHERE fnxs.FECHA_INGRESO >= TRUMC(SYSDATE) - 1
AND fnxs.FECHA_INGRESO < TRUNC(SYSDATE);
Upon looking closer, the OP's date-like column might be a VARCHAR2 (could still be a date that was implicitly cast in the comparison he gave). If it is a VARCHAR, then it needs to be converted first (using an appropriate format string):
SELECT * FROM your_table
WHERE TO_DATE(fnxs.FECHA_INGRESO, 'DD-MON-YY') >= TRUMC(SYSDATE) - 1
AND TO_DATE(fnxs.FECHA_INGRESO, 'DD-MON-YY') < TRUNC(SYSDATE);
Assuming your column is of type DATE
SELECT *
FROM TABLE_NAME
WHERE FECHA_INGRESO BETWEEN TRUNC( SYSDATE ) - 1
AND TRUNC( SYSDATE );
If it is a character string then:
SELECT *
FROM TABLE_NAME
WHERE TO_DATE( FECHA_INGRESO, 'DD-MON-YY' )
BETWEEN TRUNC( SYSDATE ) - 1
AND TRUNC( SYSDATE );

Oracle Timestamp, Max and Minimal Values

I was searching, also in the Oracle Doc, for the following:
What is the range for Timestamp in Oracle?
I know for date it is -4712, Jan-01 to 9999 Dec-31, but what for Timestamp?
Anyone a clue or hint where I can search?
You can always just try it:
SQL> select to_timestamp( '9999-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss' ) from dual;
TO_TIMESTAMP('9999-12-3123:59:59','YYYY-MM-DDHH24:MI:SS')
---------------------------------------------------------------------------
31-DEC-99 11.59.59.000000000 PM
and:
SQL> select to_timestamp( '9999-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss' )+1 from dual;
select to_timestamp( '9999-12-31 23:59:59', 'yyyy-mm-dd hh24:mi:ss' )+1 from dual
*
ERROR at line 1:
ORA-01841: (full) year must be between -4713 and +9999, and not be 0
It would be surprising if the range for the DATE portion of a TIMESTAMP was smaller than the range for a DATE, so it should be:
-4712-01-01 00:00:00 to 9999-12-31 23:59:59.999999
That assumes no time zone; the UTC value is probably constrained to that range, but someone in an Eastern time zone might manage to see a data value on 1000-01-01 in their time zone.
It is hard to find definitive data off Oracle's site. The best I found in a casual survey was:
http://www.techonthenet.com/oracle/datatypes.php
There are probably others.
I found a quote which says:
TIMESTAMP Datatype
The TIMESTAMP datatype is an extension of the DATE datatype. It stores the year,
month, and day of the DATE datatype, plus hour, minute, and second values.

Oracle date format problem

I have the following strange problem in Oracle
(Please keep in mind that I have little experience in SQL and even less in Oracle).
If I do this:
SELECT TO_CHAR(sysdate, 'YYYY-MM-DD HH24:MI') FROM dual
I get this:
2010-12-02 18:39
All fine there.
However, if I do this:
UPDATE favorite_item
SET favorite_item.last_used_date = TO_DATE(sysdate, 'YYYY-MM-DD HH24:MI')
WHERE favorite_item.favorite_item_id = 1
I get this in my database:
10-DEC-02
Which is the 10th of December '02 which is not correct
If I do this to confirm:
SELECT TO_CHAR(favorite_item.last_used_date, 'YYYY-MM-DD HH24:MI') AS last_used_date
FROM favorite_item
WHERE favorite_item.favorite_item_id = 1
I get this:
0002-12-10 00:00
Which is completely wrong.
What am I doing wrong? I feel that the date setting is not working correctly.
Thanks in advance for your help.
Don't use TO_DATE() on sysdate; sysdate is already a date.
UPDATE favorite_item
SET favorite_item.last_used_date = sysdate
WHERE favorite_item.favorite_item_id = 1`
The problem is using the to_date() function on anything other than a string.
As to why you are getting the wrong results, there is an internal conversion that happens when you use to_date on a date. Since to_date actually takes input as a string, your date is initially converted into a string (according to your NLS_DATE_FORMAT setting) and then converted back to a date. Hence the mismatch.
SQL> select sysdate from dual;
SYSDATE
---------
02-DEC-10
SQL> select to_date(sysdate,'YYYY-MM-DD') from dual;
TO_DATE(S
---------
10-DEC-02
--- This is because, the above string is actually executed as
SQL> select to_date(
to_char('02-DEC-10','YYYY-MM-DD') from dual;
TO_DATE('
---------
10-DEC-02
SQL> select to_date(
2 /* implicit conversion... dd-mon-yy' is my session's NLS_DATE_FORMAT */
3 to_char(sysdate,'dd-mon-yy'),
4 'YYYY-MM-DD')
5 from dual;
TO_DATE(/
---------
10-DEC-02
sysdate returns a date, so converting it to a date using to_date(sysdate, ...) is redundant/not necessary. You're getting that odd result because the date is being cast to a string by the to_date function using the Oracle default of "DD-MON-YY" and then back into a date using your supplied format, "YYYY-MM-DD". Since the formats don't match, Oracle is interpreting the year as the day and the day as the year. This works correctly (but, again, is redundant):
select to_date(sysdate, 'DD-MON-YY') from dual;

Resources