The between and like functions with dates - oracle

I ran these two scripts below and they give two different results. The first did not include data for the 30th of April but the latter does. I am using oracle sql. Could someone assist?
select distinct * from a where (m_date between'01-MAY-17' AND '30-MAY-17');
select distinct * from a where m_date like '%-MAY-17';

I used the to_date function and it worked:
select distinct * from a where to_date (m_date) between'01-MAY-17' AND '30-MAY-17'
This produced the same results as with the like clause:
select distinct * from a where m_date like '%-MAY-17'

Your between clause
select distinct * from a where to_date (m_date) between'01-MAY-17' AND '30-MAY-17'
is the same as saying (pseudo-code)
...where to_date (m_date) between '01-may-17 00:00:00' AND '30-may-17 00:00:00'...
which excludes and date values on 5/30 where there is any time element other than 12AM in the morning. (Also, May does have 31 days).
It would be more correct to do the following, assuming m_date is a date data type and assuming you want all values during the month of may.
... where m_date >= to_date('01-may-17') and m_date < to_date('01-jun-17')...

Related

How do I separate the time and date in SQL navigator?

I am trying to separate the time and date in one column to be independent off each other. I am new at writing scripts
this is my query:
select
*
from
[tablename]
where
to_date([column_name]) in ( '15-Jun-2021', '16-Jun-2021' )
and
to_char([column_name],'dd-Mon-yyyy HH:MM:ss') < '15-Jun-2021 19:54:30'
The way you put it, it would be
select *
from your_table
where date_column >= date '2021-06-15'
and date_column < to_date('15.06.2021 19:54:30', 'dd.mm.yyyy hh24:mi:ss')
because
date_column should be of date datatype. If it isn't, you'll have problems of many kinds in the future. Therefore,
don't to_date it, it is already a date
don't to_char it either, because you'd be comparing strings and get unexpected result. Use that function when you want to nicely display the result
the second condition you wrote makes the first one questionable. If date_column is less than value you wrote, then you can omit date '2021-06-16' from the first condition because you won't get any rows for that date anyway
date literal (date '2021-06-15') sets time to midnight, so condition I wrote should return rows you want
SQL> select date '2021-06-15' first,
2 to_date('15.06.2021 19:54:30', 'dd.mm.yyyy hh24:mi:ss') second
3 from dual;
FIRST SECOND
------------------- -------------------
15.06.2021 00:00:00 15.06.2021 19:54:30
SQL>

Oracle date comparison using to_date not working

I am using Oracle XE on my machine.
Defined a table as :
Name Type
ENAME VARCHAR2(20)
DOJ DATE
Firing a simple select:
select * from test1.tasty1;
ENAME DOJ
sat 08-DEC-16
So ok - I am aware that DATE field has time component in it.
The following query using TRUNC works fine:
select * from test1.tasty1 where trunc(DOJ) = '08-DEC-16';
Now I wanted to test the 'to_date' function - but none of the below queries worked - wonder why ?
select * from test1.tasty1 where DOJ =
to_date('08-12-2016','DD-MM-YYYY');
select * from test1.tasty1 where
DOJ = to_date('08-DEC-2016','DD-MON-YYYY');
select * from
test1.tasty1 where DOJ = to_date('08-DEC-16','DD-MON-YY');
select
* from test1.tasty1 where DOJ = to_date('08-DEC-16','dd-mon-RR');
Had taken a look at the following on SO:
Oracle TO_DATE not working
so not sure what is wrong here ?
From your question and comments, it appears that this is the sequence of events which happened.
You did the following INSERT into your table:
INSERT INTO test1.tasty1 VALUES ('sat', SYSDATE)
Keep in mind that dates in Oracle have both a date and a time component. So even though you did insert the date '2016-12-08' you also inserted a time component. As a result, the following query is not returning any records:
SELECT * FROM test1.tasty1 WHERE DOJ = '2016-08-12'
This is probably because you never specified the time component, and therefore the record you inserted earlier is not matching. If you want to compare only the date portion, you can use TRUNC as follows:
SELECT * FROM test1.tasty1 WHERE TRUNC(DOJ) = '2016-08-12'
The solution to your problem moving forward would be to wrap SYSDATE with TRUNC during the insert, if you really only want to deal with the date components.
By the way, the format '08-DEC-16' used as a literal will not be recognized by Oracle as a valid date. Instead, use '2016-12-08'.
Have you tried like this as comparison of date with date is correct:
select * from test1.tasty1 where to_date(DOJ,'DD-MM-YYYY') = to_date('08-12-2016','DD-MM-YYYY');
Compare apples with apples and not with mangoes.
What it worked from is instead of make a date to compare, change the date column to char with to_char(datecol, 'DD-MM-YYYY') = '01-01-2022'

Query sysdate against field in SQL Developer

SELECT LAST_MAINTAIN, PLANE_ID
FROM PLANE
WHERE MONTHS BETWEEN(
(TO_DATE('sysdate', 'MM/DD/YYYY'),
TO_DATE('LAST_MAINTAIN', 'MM/DD/YYYY')))
>24;
I am trying to select Last_Maintain, a date field, from the Plane table and return results that have 2 years between today's date and the last maintenance. Does anyone know what is wrong? I have a feeling I am using months between incorrectly but I'm not sure. Thanks
Edit: Adjusted location of sysdate and Last_Maintain, still erroring out saying I'm missing a parentheses (right), even though I went through all my parentheses without seeing one missing.
Edit2: Tried out
SELECT *
FROM plane
WHERE MONTHS BETWEEN (sysdate, last_maintain ) > 24;
Still telling me I need more parentheses.
Edit3: Problem is resolved, here is the final code that worked:
SELECT *
FROM plane
WHERE MONTHS_BETWEEN (sysdate, last_maintain) > 24;
Don't call to_date on a date. to_date converts a string to a date. It would only make sense to pass a string to to_date.
sysdate is a function that returns a date. That is very, very different from a literal string sysdate
You probably want
SELECT *
FROM plane
WHERE months_between( sysdate, last_maintain ) > 24
MONTHS_BETWEEN returns the result as first - second, so in your case, all values are < 0. Put sysdate first.
From Oracle doc:
"If date1 is earlier than date2, then the result is negative"

Oracle Recursive Query - Dates

I've got two sets of dates being passed into a query and I would like to find all the months/years between both sets of dates.
When I try this:
WITH CTE_Dates (cte_date) AS (
SELECT cast(date '2014-01-27' as date) from dual
UNION ALL
SELECT cast(ADD_MONTHS(TRUNC(cte_date, 'MONTH'),1) as date)
FROM CTE_Dates
WHERE ( TO_DATE(ADD_MONTHS(TRUNC(cte_date, 'MONTH'), 1)) BETWEEN TO_DATE ('27-01-2014','DD-MM-YYYY') AND TO_DATE ('27-04-2014','DD-MM-YYYY'))
OR
( TO_DATE(ADD_MONTHS(TRUNC(cte_date, 'MONTH'), 1)) BETWEEN TRUNC(TO_DATE('27-11-2014','DD-MM-YYYY'), 'MONTH') AND TO_DATE ('27-01-2015','DD-MM-YYYY'))
)
SELECT * from CTE_Dates
I get:
27-JAN-14
01-FEB-14
01-MAR-14
01-APR-14
I would also want to get:
01-NOV-14
01-DEC-14
01-JAN-15
It looks like the OR portion of the WHERE clause gets ignored.
Suggestions on how to create this query?
Thanks
Cory
The problem with what you have now (aside from extra cast() and to_date() calls) is that on the fourth iteration both the conditions are false so the recursion stops; there's nothing to make it skip a bit and pick up again, otherwise it would continue forever. I don't think you can achieve both ranges within the recursion.
You can put the latest date you want inside the recursive part, and then filter the two ranges you want afterwards:
WITH CTE_Dates (cte_date) AS (
SELECT date '2014-01-27' from dual
UNION ALL
SELECT ADD_MONTHS(TRUNC(cte_date, 'MONTH'), 1)
FROM CTE_Dates
WHERE ADD_MONTHS(TRUNC(cte_date, 'MONTH'), 1) <= date '2015-01-27'
)
SELECT * from CTE_Dates
WHERE cte_date BETWEEN date '2014-01-27' AND date '2014-04-27'
OR cte_date BETWEEN date '2014-11-27' AND date '2015-01-27';
CTE_DATE
---------
27-JAN-14
01-FEB-14
01-MAR-14
01-APR-14
01-DEC-14
01-JAN-15
6 rows selected
You can replace the hard-coded values with your pairs of start and end dates. If the ranges might overlap or the second range could be (or end) before the first one, you could pick the higher date:
WHERE ADD_MONTHS(TRUNC(cte_date, 'MONTH'), 1)
<= greatest(date '2015-01-27', date '2014-04-27')
... though that only makes sense with variables, not fixed values.

how to catch this 20140205101309 date format?

I wanna catch the date in my table which is written like this "20140205101309" I tried to get by many ways, but i was not able to catch it so how to modifiy my query to catch it ?
select * from my.DETAIL a where A.DATE
between to_DATE('YYYYMMDD hh24:mi:ss','28-dec-2013 12:00:00')
and to_date ('YYYYMMDD hh24:mi:ss','2-feb-2014 00:00:00');
thank you in advance
Make a.call_date a date before comparing:
select *
from operation.reject_detail a
where to_date(a.call_date, 'YYYYMMDDHHMISS') between
to_date('28-12-2013 12:00:00','DD-MM-YYYY HH24:MI:SS') and
to_date('02-02-2014 00:00:00','DD-MM-YYYY HH24:MI:SS' );
Assuming 20140205101309 is: 2014, februari, 5 10:13:09
If you are not interested in the time part do this:
select *
from operation.reject_detail a
where to_date(substr(a.call_date,1,8), 'YYYYMMDD') between
to_date('28-12-2013','DD-MM-YYYY') and
to_date('02-02-2014','DD-MM-YYYY');
Storing dates as strings is bad practice, but since that is what you have and it's in a relatively sensible format, you can just compare it as a string:
select * from my.DETAIL a
where A.DATE between '20131228120000' and '20140202000000'
Or to ignore the time part on the table and include the full second date:
select * from my.DETAIL a
where A.DATE between '20131228000000' and '20140202235959'
There doesn't seem to be much point converting everything to DATE types, particularly if your column is indexed. If it isn't indexed than you could just look at the date part:
where substr(A.DATE, 1, 8) between '20131228' and '20140202'

Resources