"ORA-01843: Not a vaild month" when comparing insert_date to String - sql

I have a table that uses sysdate to assign column "insert_date" the current date and time upon inserting records into the table. I'm trying to use a query to check for records depending on the day they were inserted:
select insert_date
from integration.ol_order
where trunc(insert_date) like '%5/1/2015%'
Upon running the query, I get "ORA-01843: Not a vaild month" even though trunc(insert_date) outputs '5/1/2015'.
This occurs for the following where statements as well:
where trunc(insert_date) like '5/1/2015%'
where trunc(insert_date) = '5/1/2015'
I must be missing something simple here.

TRUNC - when applied to date - returns a date, so you should compare to a date:
where trunc(insert_date) = date '2015-01-05'
This uses a DATE literal to create a date from an ISO date string (YYYY-MM-DD).
However, this will apply TRUNC() to every row in your table, thus hurting performance. A much better solution is to compare your column to an interval:
where insert_date between date '2015-01-05' and date '2015-01-06'
UPDATE
As Wernfried pointed out, this will include '2015-01-06 00:00:00'. If that's a problem, you can either subtract a minimal timespan from the upper bound:
where insert_date between date '2015-01-05'
and date '2015-01-06' - interval '1' second
or use <= and < instead of between :
where date '2015-01-05' <= insert_date
and insert_date < date '2015-01-06'
SQL Fiddle

"LIKE" only works with character fields, not datetime fields. Also, the date format for queries is YYYY-MM-DD. To test for a date for a specific day:
insert_date BETWEEN '2015-05-01' AND '2015-05-01:23:59:59'

Related

how to select all entries having date 25-11-20 in oracle 11g?

sql table
here in the table above named carpooling contains a column name start_on which has date time as timestamp i have to write a query to select all the rows having date as 25-11-20 using to_char and to_date.
You write a timestamp literal like this:
timestamp '2020-11-25 00:00:00'
so the full filtering condition will be
where start_on >= timestamp '2020-11-25 00:00:00'
and start_on < timestamp '2020-11-26 00:00:00'
Note that dates and timestamps are different in Oracle, and dates include times down to the second (this is for historical reasons - originally there was only the date type, and timestamp was added much later).
Use the TRUNC function, along with date and interval literals:
SELECT *
FROM CARPOOLING
WHERE START_ON BETWEEN DATE '2020-11-25'
AND (DATE '2020-11-26' - INTERVAL '0.000001' SECOND)
You can simply use to_date, but it's recommended to remove the time when comparing the dates. Otherwise, rows having the same date, but a different time will not be selected. Removing the time can be done using TRUNC.
So you can do something like this:
SELECT * FROM carpooling
WHERE TRUNC(start_on) = TO_DATE('2020-11-25','yyyy.mm.dd');
If you don't want to check the 25th of November 2020, but another data, change the date to match your goal.

Changing date to day before for certain daily time ranges in SQL

We have a table with a datetime field and need to get it to work properly with other tables that have date fields that work on system date, not calendar date.
Our system day runs until 1:59 AM, so anything after midnight until then is considered the day before in all of these tables (which don't actually show datetime, just date).
I am trying to figure out how I can create a date field where '2019-01-01 01:45:00.111' would read as '2018-12-31', but '2019-01-01 2:00:00.111' would read as '2019-01-01'.
Any ideas would be much appreciated.
Try this simple logic below by removing 120 minutes from your each datetime value and then cast the datetime value as date.
SELECT CAST(DATEADD(MINUTE,-120,<your_date_time_column>) AS DATE)
FROM <Your_Table>
You can make use of CASE WHEN.. clause to increment or decrement dates by 1 like below sample
SELECT CASE WHEN TO_CHAR(DATE,
'HH:MM') BETWEEN '00:00' AND
'01:45' THEN DATE-1 ELSE
DATE END FROM TABLE

Oracle - Convert datetime format

I have a temp table.
It has last_update column in 2/10/2018 6:01:50 PM datetime format.
How can I write THE BEST QUERY to display all information that's updated on 02-Oct-2018 day?
You can use trunc function
select *
from tab
where trunc(last_update) = date'2018-10-02'
It is preferable to avoid TRUNC especially if you have an index on the column last_update.
A simple where condition should be better and may be better performant.
WHERE last_update >= date '2018-10-02' AND
last_update < date '2018-10-02' + 1
Use trunc function for getting the same day:
trunc(last_update) = trunc(to_date('02-Oct-2018', 'DD-MONTH-YYYY'))
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day.
You can also use format DD-MON-YYYY

TIMESTAMP To DATE in Oracle SQL

I have a column called Create_Date which has data in the format like 19-JUN-18 10.27.00.000000000 PM and data type is TIMESTAMP(6).
I am trying to look at date range like yesterday's date or between two dates in Create_Date without using TO_DATE(TO_CHAR(P.CREATE_DATE_TIME,'dd/mon/yy')) and entering the value as '19-JUN-18'.
I want to use Create_Date=SYSDATE-1 OR Create_Date=CURRENT_DATE-1 instead to filter on yesterdays date. Or Use Create_Date>=SYSDATE or Create_Date>=CURRENT_DATE to look at dates greater than or equal to today.
Can someone help?
You could use TRUNC:
SELECT *
FROM tab
WHERE Create_Date >= TRUNC(SYSDATE,'DD') -- -1
-- or between to dates (using date literals)
WHERE Create_Date >= DATE 'yyyy-mm-dd'
AND Create_Date < DATE 'yyyy-mm-dd'
As it's a timestamp I'd cast the truncated (to midnight) current date to a timestamp for clarity; Oracle will use an index on that column even if you leave it as a date, but it doesn't hurt to make it explicit:
where create_date >= cast(trunc(sysdate) as timestamp)
The trunc() function defaults to truncating to midnight; you can explicitly include 'DD' as a second argument if you prefer (for even more clarity, though some would see it as noise).
If you want a range, say yesterday:
where create_date >= cast(trunc(sysdate) - 1 as timestamp)
and create_date < cast(trunc(sysdate) as timestamp)
If you want to specify other dates then you can use timestamp literals, e.g. to see everything for May:
where create_date >= timestamp '2018-05-01 00:00:00'
and create_date < timestamp '2018-06-01 00:00:00'

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'