Sql strictly more than query - sql

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'

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.

Not a Valid Month - Working with Dates in Oracle

I am finding it strenuous to work with dates in my customized environment. I have a request to add a where clause which caters to specific dates but I just cannot get oracle to budge. Any ideas anyone please.
select created_date, cast(created_date as date) as created_date_cast
from mytable;
created_date created_date_cast
04-Mar-20 05.21.15.772000 AM 3/4/2020 5:21:15 AM
04-Mar-20 05.21.15.709000 AM 3/4/2020 5:21:15 AM
04-Mar-20 05.17.14.902000 AM 3/4/2020 5:14:14 AM
28-Feb-20 01.15.25.702700 AM 2/28/2020 1:15:25 AM
When I try to add a where clause the snippet blows up with the error:
select created_date, cast(created_date as date) as created_date_cast
from mytable
where cast(created_date as date) <= '02/28/2020';
ORA-01843: not a valid month
I have also tried to_date(created_date, 'MM/DD/YYYY') in the from but proves to be erroneous with:
ORA-01858: a non-numeric character was found where a numeric was expected
Firstly cast as date which converts a timestamp value to a date value, and then don't forget to add trunc() function in order to include the boundry value (date'2020-02-28' in this case) also as
where trunc(cast(created_date as date)) <= date'2020-02-28'
Demo
Don't use CAST and don't use TRUNC (as then Oracle will not be able to use an index on your column but would, instead, require a function-based index created on TRUNC(created_date)) just add a day and use a literal:
SELECT created_date
FROM mytable
WHERE created_date < DATE '2020-02-29';
or
SELECT created_date
FROM mytable
WHERE created_date < TIMESTAMP '2020-02-29 00:00:00';
or, if you want to specify the exact date then just add a day. E.g.:
SELECT created_date
FROM mytable
WHERE created_date < DATE '2020-02-28' + INTERVAL '1' DAY;
All of those options should be able to use an index on the created_date column.
I have also tried to_date(created_date, 'MM/DD/YYYY') in the from but proves to be erroneous with:
ORA-01858: a non-numeric character was found where a numeric was expected
TO_DATE( value_string, format_model ) takes strings as its arguments but CREATED_DATE is a TIMESTAMP data type and not a string so Oracle must make an implicit TIMESTAMP-to-string conversion and it does this using the NLS_TIMESTAMP_FORMAT session parameter; so your expression is effectively:
TO_DATE(
TO_CHAR(
created_date,
( SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_TIMESTAMP_FORMAT' )
),
'MM/DD/YYYY'
)
And if your NLS_TIMESTAMP_FORMAT is not MM/DD/YYYY then its highly likely that an exception will be raised (i.e. like the ORA-01858 you had).
You should never rely on implicit string conversions as any user can change their own session parameters at any time and an implicit conversion that works for one user may not work for another just because they have different parameter values (even though the queries are identical).
Use a date literal:
cast(created_date as date) <= date '2020-02-28'
I would also recommend dispensing with the cast() -- assuming that created_date is correctly stored as a date or timestamp:
created_date < (date '2020-02-28' + interval '1' day)

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'

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.)

how to use = assignment operator with timestamp date column in oracle

I'm using timestamp in dat column in table r3. when I fire command
select dat from r3 where dat='16-nov-09';
it shows "no rows selected" but when i fire command
select dat from r3 where dat>'15-nov-09';
it shows the whole data of 16-nov-09. Tell me what is wrong in my first command or what i have to do.
Quering on oracle date columns is always confusing. The date columntype is always a datetime. Storing the current date from sysdate stores always the time component too.
There good and evil ways quering the date columns. I show and vote some.
where to_char(DAT, 'DD-MON-YYYY') = '16-NOV-2009'
where trunc(DAT) = to_date('16-NOV-2009', 'DD-MON-YYYY')
Both bad, because they do not use any index. To avoid this, you can define a function based index on the expression.
The trick of both is to cut off the time component. If time is not needed, than it is a good advise to cut off the time in INSERT and UPDATE trigger. The function based index can convert to a normal index.
where DAT between to_date('16-NOV-2009', 'DD-MON-YYYY')
and to_date('16-NOV-2009 23:59:59', 'DD-MON-YYYY HH24:MI:SS')
where DAT >= to_date('16-NOV-2009', 'DD-MON-YYYY') and DAT < to_date('16-NOV-2009', 'DD-MON-YYYY')+1
This two are always my favorites.
Its a good advice to use to_date and to_char to convert the values between string and datetime.
As DAT is timestamp you can use as below
select DAT from R3
where DAT between to_date('16-NOV-09' , 'dd-MON-yy') and to_date('16-NOV-09 23:59:59', 'DD-MON-YY hh24:mi:ss')
Timestamp has time and date components, so query
select dat from r3 where dat='16-nov-09';
will work only for records where time component is midnight: '00:00:00'
Beside formatting (to_date function), you can truncate timestamp to get only date:
select dat from r3 where trunc(dat)='16-nov-09';
Beware that this will not use index on field dat (if there is any).
TIMESTAMP and DATE are different data types in oracle and both store time components. If you really do need to store subsecond times then you use TIMESTAMP, otherwise DATE is your best choice.
The ANSI timestamp and date literal syntaxes are quite handy:
create table ts_test (ts1 timestamp);
select *
from ts_test
where ts1 > timestamp '2009-10-11 00:00:00'
/
select *
from ts_test
where ts1 > timestamp '2009-10-11 00:00:00.1'
/
select *
from ts_test
where ts1 > timestamp '2009-10-11 00:00:00.001'
/
select *
from ts_test
where ts1 = date '2009-10-11'
/
use the below format for a date field in where condition.
where to_char(DAT,'mmddyyyy') = '11152009';
In Oracle the date fields also contain a time component, so 16-nov-09 is actually midnight of Nov 16th.
Two different ways to handle this:
where to_char(DAT,'mmddyyyy') = '11152009'
as john suggested, but I like the following version more:
where trunc(dat) = to_date ('11152009', 'mmddyyyy')
TRUNCfor a date "removes" the time component (or to be more specific, truncates it to midnight), and to_date is the proper way to construct a date value in Oracle SQL. (I prefer to do the comparisons in the right domain - DATEs as in the second example- over another - STRINGs as in the first example. With strings you may run into some weird month issues, sorting is easier in dates etc.)
Just to add to it , An easy way out when you are not bothered about the time-stamp but just want to compare the date is to use the 'like' operator.
for example
select dat from r3 where dat LIKE '16-nov-09%'
will give you desired output.