Result empty when date variable - sql

I'm using an Oracle 10g Database. I have a table with a lot of information. One of the columns has a type of DATE declared as follows DATE_INSERT DATE. I'm trying to make a query and have it filtered by a specific Date. When I use the function TO_CHAR() in the where clause I get the information as expected
SELECT * FROM TABLE WHERE TO_CHAR(DATE_INSERT, 'DD/MM/YYYY') = '05/10/2018'
But when I use a DATE variable or the TO_DATE() function I get an empty result set.
SELECT * FROM TABLE WHERE DATE_INSERT = TO_DATE('05/10/2018','DD/MM/YYYY')
OR
SELECT * FROM TABLE WHERE DATE_INSERT = date '2018-10-05';
OR (date_var defined as a date previously)
SELECT * FROM TABLE WHERE DATE_INSERT = date_var;
By a DB department requirement I need to get rid of all functions TO_DATE() / TO_CHAR(). Can you help me know why is the filtering not working when DATE types are used in the query?

This is due to the time component. I would recommend either:
WHERE DATE_INSERT >= date '2018-10-05' AND
DATE_INSERT < date '2018-10-06'
Or:
WHERE TRUNC(DATE_INSERT) = date '2018-10-05'
Oracle supports function-based indexes. If you want an index to be used, you can create an index to support either of these queries.

In Oracle, a DATE column always stores the date and the time information. So the query you mention:
SELECT * FROM TABLE WHERE TO_CHAR(DATE_INSERT, 'DD/MM/YYYY') = '05/10/2018'
doesn't use equality, but queries for a range. Which range? The whole day, that is 24 hours.
For the other query to you'll need to use a range, as in:
SELECT * FROM TABLE
WHERE DATE_INSERT >= TO_DATE('05/10/2018','DD/MM/YYYY')
AND DATE_INSERT < TO_DATE('06/10/2018','DD/MM/YYYY')
More clearly, to show the date & time info:
SELECT * FROM TABLE
WHERE DATE_INSERT >= TO_DATE('05/10/2018 00:00:00','DD/MM/YYYY HH24:MI:SS')
AND DATE_INSERT < TO_DATE('06/10/2018 00:00:00','DD/MM/YYYY HH24:MI:SS')

Related

Filter by date in oracle SQL

I have a table with a date column (DD/MM/YYYY hh:mm:ss) as follows:
02/09/2021 09:50:37
03/09/2021 09:20:27
02/09/2021 14:05:25
03/09/2021 12:50:28
02/09/2021 14:25:26
What's the most efficient way to filter by date?
Note: I tried with
select date
from table
where date = '02/09/2021'
However, this provides results that wrongly lack the time and only contain the date 02/09/2021.
For example, 02/09/2021 09:50:37 would not be returned.
A simple option is
select *
from your_table
where trunc(date_column) = date '2021-09-02'
If there's an index on date_column, it wouldn't be used in that case so performance might suffer so you'll have to either create a function-based index, or use a different where clause:
where date_column >= date '2021-09-02' and date_column < date '2021-03-03'
If you want to return every record that has some date column equal to some date regardless to the time part then you need this:
select date
from table
where trunc(date) = '02/09/2021'
please avoid naming columns date and tables table.
Here is a small demo
Use a date range:
SELECT date_column
FROM table_name
WHERE date_column >= DATE '2021-09-02'
AND date_column < DATE '2021-09-02' + INTERVAL '1' DAY;
This will enable Oracle to use an index on the date_column column.
If you filter using:
TRUNC(date_column) = DATE '2021-09-02' or
TO_CHAR(date_column, 'DD-MM-YYYY') = '02-09-2021'
Then Oracle will not use an index on the date_column and would need a separate function-based index on either TRUNC(date_column) or TO_CHAR(date_column, 'DD-MM-YYYY').

Get Records By Todays Date And Before Current Time

I am trying to query some records and am looking to return only records that have a date of today, but also are before the current time.
I have the first part sorted out by using the following clause to return only records for today.
WHERE TRUNC(my_date_time) = TRUNC(sysdate)
How would I modify this to only get records before the current system time as well?
How about inequalities?
WHERE my_date_time >= TRUNC(sysdate) AND
my_date_time < sysdate
Try this.
Select * from table Where
TRUNC(my_date_time) =
TRUNC(sysdate) And
TO_CHAR(my_date_time,
'HH24:MI:SS' ) <=
TO_CHAR(sysdate,
' HH24:MI:SS' )
If column "my_date_time" is of type DATE, and need compare using SYSDATE function then you only need:
WHERE my_date_time<=sysdate
If column "my_date_time" is of type DATE, and need compare with other variable or column of type DATE for example named "other_date"
where my_date_time>=trunc(other_date)
and my_date_time<trunc(other_date)+1

Sql strictly more than query

I'm in PostgreSQL.
I need to print all mailing with creation date strictly more that 2015-04-04. I tried the following queries:
SELECT *
FROM mailing.mailing
WHERE creation_date > '2015-04-04';
and
SELECT *
FROM mailing.mailing
WHERE creation_date >= '2015-04-04';
But they produced the same result set(including '2015-04-04'). Is it possible to write such a query without explicitly saying WHERE creation_date >= '2015-04-05';
UPD: The column's type is timestamp without time zone.
If your creation_date field is of type datetimetry comparing it to '2015-04-04 23:59:59' instead, as '2015-04-04 08:30:00' seems to be greater than '2015-04-04'.
Assuming your default date format for your database is 'YYYY-MM-DD' and creation_date field is a date type, your query will actually be converted automatically to something like:
SELECT *
FROM mailing.mailing
WHERE creation_date > to_date('2015-04-04', 'YYYY-MM-DD');
The date value you have provided represents the first second of that day, that's why you see no difference between your queries. (Your first query would exclude the first second of the day though.)
What you could do to avoid this is:
where creation_date >= to_date('2015-04-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
or
where date_trunc(creation_date-1) = '2015-04-04'

How to select from SQL table WHERE a TIMESTAMP is a specific Date

Im trying to do a query where a TIMESTAMP field is = to a specific date but my query is not working:
The field is type TIMESTAMP(6) which I have only ever worked with DATE / DATETIME fields before. Here is example of a value stored here: 04-OCT-13 12.29.53.000000000 PM
Here is my SELECT statement:
SELECT * FROM SomeTable WHERE timestampField = TO_DATE('2013-10-04','yyyy-mm-dd')
I am retrieving no results and I am assuming it has to do with the fact that its not matching the TIME portion of the timestamp
If you want every record that occurs on a given day then this should work:
SELECT * FROM SomeTable
WHERE timestampField >= TO_TIMESTAMP( '2013-03-04', 'yyyy-mm-dd' )
AND timestampField < TO_TIMESTAMP( '2013-03-05', 'yyyy-mm-dd')
That will be likely to take advantage of an index on timestampField if it exists. Another way would be:
SELECT * FROM SomeTable
WHERE TRUNC(timestampField) = TO_DATE( '2013-03-04', 'yyyy-mm-dd' )
in which case you may want a function-based index on TRUNC(timestampField).
(Note that TRUNC applied to a TIMESTAMP returns a DATE.)

Select from table by knowing only date without time (ORACLE)

I'm trying to retrieve records from table by knowing the date in column contains date and time.
Suppose I have table called t1 which contains only two column name and date respectively.
The data stored in column date like this 8/3/2010 12:34:20 PM.
I want to retrieve this record by this query for example (note I don't put the time):
Select * From t1 Where date="8/3/2010"
This query give me nothing !
How can I retrieve date by knowing only date without the time?
DATE is a reserved keyword in Oracle, so I'm using column-name your_date instead.
If you have an index on your_date, I would use
WHERE your_date >= TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND your_date < TO_DATE('2010-08-04', 'YYYY-MM-DD')
or BETWEEN:
WHERE your_date BETWEEN TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND TO_DATE('2010-08-03 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
If there is no index or if there are not too many records
WHERE TRUNC(your_date) = TO_DATE('2010-08-03', 'YYYY-MM-DD')
should be sufficient. TRUNC without parameter removes hours, minutes and seconds from a DATE.
If performance really matters, consider putting a Function Based Index on that column:
CREATE INDEX trunc_date_idx ON t1(TRUNC(your_date));
Personally, I usually go with:
select *
from t1
where date between trunc( :somedate ) -- 00:00:00
and trunc( :somedate ) + .99999 -- 23:59:59
Convert your date column to the correct format and compare:
SELECT * From my_table WHERE to_char(my_table.my_date_col,'MM/dd/yyyy') = '8/3/2010'
This part
to_char(my_table.my_date_col,'MM/dd/yyyy')
Will result in string '8/3/2010'
You could use the between function to get all records between 2010-08-03 00:00:00:000 AND 2010-08-03 23:59:59:000
trunc(my_date,'DD') will give you just the date and not the time in Oracle.
Simply use this one:
select * from t1 where to_date(date_column)='8/3/2010'
Try the following way.
Select * from t1 where date(col_name)="8/3/2010"