I have a datetime field in my Postgresql, named "dt".
I'd like to do something like
SELECT * FROM myTable WHERE extract (date from dt) = '01/01/11'
What is the right syntax to do that?
Thanks!
I think you want to cast your dt to a date and fix the format of your date literal:
SELECT *
FROM table
WHERE dt::date = '2011-01-01' -- This should be ISO-8601 format, YYYY-MM-DD
Or the standard version:
SELECT *
FROM table
WHERE CAST(dt AS DATE) = '2011-01-01' -- This should be ISO-8601 format, YYYY-MM-DD
The extract function doesn't understand "date" and it returns a number.
With PostgreSQL there are a number of date/time functions available, see here.
In your example, you could use:
SELECT * FROM myTable WHERE date_trunc('day', dt) = 'YYYY-MM-DD';
If you are running this query regularly, it is possible to create an index using the date_trunc function as well:
CREATE INDEX date_trunc_dt_idx ON myTable ( date_trunc('day', dt) );
One advantage of this is there is some more flexibility with timezones if required, for example:
CREATE INDEX date_trunc_dt_idx ON myTable ( date_trunc('day', dt at time zone 'Australia/Sydney') );
SELECT * FROM myTable WHERE date_trunc('day', dt at time zone 'Australia/Sydney') = 'YYYY-MM-DD';
Related
I have table in Oracle SQL Developer like below:
col1
--------
2019-11-29 11:14:00.821822
2020-02-11 09:14:00.821847
And I would like to select only rows where date is '2019-11-29' how can I do that?
You can use:
where trunc(col1) = date '2019-11-29'
However, that cannot use an index on col1. So, it is often better to use:
where col1 >= date '2019-11-29' and
col1 < date '2019-11-30'
You can to_date() or to_char() functions. The Oracle/PLSQL TO_DATE function converts a string to a date and TO_CHAR function converts a date to a String. The TRUNC(date) function returns date without the time portion.
select *
from myTable
where trunc(col1) = to_date('2019-11-29', 'yyyy-mm-dd');
or:
select *
from myTable
where to_char(col1, 'yyyy-mm-dd') = '2019-11-29'
Not sure how to use the NOW() function in presto. Seems like it should be straight forward, but i'm getting no luck
SELECT DISTINCT field
FROM table
WHERE field BETWEEN '2019-01-01' and NOW()
field = varchar
You should match data types to avoid implicit conversions:
SELECT DISTINCT field
FROM table
WHERE CAST(field AS DATE) BETWEEN DATE '2019-01-01' AND CURRENT_DATE;
SELECT DISTINCT field
FROM table
WHERE DATE(field) BETWEEN DATE '2019-01-01' AND CURRENT_DATE;
I am trying to get the row from the Database which saves the date in the following format 2018-07-09 13:22:40
When I write the following query like this:-
SELECT *
FROM table_name
WHERE time_col = '2018-07-09 13:22:40';
it returns a single row
But When I write a query like this (no result):
SELECT *
FROM table_name
WHERE time_col = '2018-07-09';
it returns me nothing.
For the other operators, it works for <, >, <=, >= with the following query but why not with "=" Operator
SELECT *
FROM table_name
WHERE time_col < '2018-07-09';
Assuming your column is of type timestamp, then the string constant '2018-07-09' will be converted to a timestamp by Postgres - but with the time as 00:00:00.
So the following query:
SELECT *
FROM table_name
WHERE time_col = '2018-07-09';
is actually run as:
SELECT *
FROM table_name
WHERE time_col = timestamp '2018-07-09 00:00:00';
Which of course does not match the value 2018-07-09 13:22:40.
To get the rows on a specific day, you can cast the timestamp column to a date:
SELECT *
FROM table_name
WHERE time_col::date = date '2018-07-09'
This will however prevent the usage on the time_col column. So if that version of the query is slow, you have to revert to a range query:
SELECT *
FROM table_name
WHERE time_col >= timestamp '2018-07-09 00:00:00'
and time_col < timestamp '2018-07-10 00:00:00'
It does not work cause column is timestamp (date with time). But you trying compare that with date(no date), that is why they are not equal.
Use trunc function in oracle. trunc function cuts time from date and leaves only date:
SELECT * FROM table_name WHERE trunc(time_col) = '2018-07-09';
And for postgres use explicit cast:
SELECT * FROM table_name WHERE time_col::date = '2018-07-09';
SELECT *FROM table
WHERE update_date >= '2013-05-03'::date
AND update_date < ('2013-05-03'::date + '1 day'::interval);
How can I convert a date to a timestamp?
My query:
SELECT * FROM CTP0421
WHERE timestamp <= '2016-04-04'
This doesn't work for me.
SELECT * FROM CTP0421
WHERE trunc(timestamp) <= to_date('2016-04-04','yyyy-mm-dd');
How do I select rows in the past starting from yesterday in Oracle DB where a field like created_date is a timestamp(6)?
I don't want to compare time, just date.
If you want exactly one day prior to the current time:
select *
from table t
where created_date < sysdate - 1;
If you want times before today:
select *
from table t
where created_date <= trunc(sysdate);
From the Oracle documentation on SELECT :
SELECT * FROM orders
WHERE created_date < TO_DATE('2014-04-28', 'YYYY-MM-DD');
I can pass this date format from my application, worked like a charm.
As you want to compare just date:
select *
from table t
where date(created_date) < DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
you can use cast function to deal with timestamp as date:
SELECT cast(SYSTIMESTAMP(6) as date)
FROM dual;
so you can select rows with "yesterdate" date by:
select ....
where cast(SYSTIMESTAMP(6) as date) like sysdate - 1
note: replace SYSTIMESTAMP(6) with column name which has timestamp type.