This question already has answers here:
Postgresql difference between two columns dates?
(3 answers)
Get month,days difference between two date columns
(1 answer)
Date column arithmetic in PostgreSQL query
(1 answer)
how to calculate only days between two dates in postgres sql query .
(2 answers)
Closed 4 years ago.
I was looking for the answer to this question, and I found here that DATEDIFF() function is a way to do it when you have the dates. But, what if you don't have specific dates?
What if you want to find the interval of time between the date in 2 columns (rental_date, return_date).
I tried to use DATEDIFF() function, but it looks like you need the actual date.
You can use date_part for finding differences between two dates in the PostgreSQL.
The syntax is like as follow.
DATE_PART('day', enddate - startdate);
You can get years, months, weeks, hours, minutes seconds as well. You can check more about the usage of date different and date part function in PostgreSQL by clicking on this link.
if your two column data type is date then you could do subtraction
select '2018-12-10'::date - '2018-11-18'::date
so in your case it would be
with t1 as
(
select '2018-12-10'::date as rental_date, '2018-11-18'::date as return_date
)
select rental_date-return_date from t1
Related
This question already has an answer here:
Oracle SQL Where clause to find date records older than 30 days
(1 answer)
Closed 2 years ago.
I want to get an ORACLE statement which returns rows that are older than 30 days from the the date of creation.
My table has a field "date_entered" that contains the date and the time it was inserted in the database .
Thanks,
your select statement
WHERE date_entered < TRUNC(SYSDATE)-30
This question already has answers here:
SQL: Get records created in time range for specific dates
(3 answers)
Closed 8 years ago.
I have a table with the field ENTERED_ON (with both date and time value). I want to write a query that return records that have ENTERED_ON value that is past 2 hours comparing to current date time.
For example, if entered_on is 2014-05-06 11:00AM, and currently it's 2014-05-06 2:00PM, I would like to return all records that past the 2 hours when comparing to current date time.
You can write query using INTERVAL. It will looks like
SELECT * FROM Table WHERE `ENTERED_ON` > DATE_ADD(NOW(), INTERVAL -2 HOUR)
This question already has answers here:
Get day of week in SQL Server 2005/2008
(11 answers)
Closed 8 years ago.
I have a list of dates and other columns in which have to find the weekend dates among them and the the weekend dates should be there in the list of given dates
Output should be some thing like this...
Any help is appricated thanks.
use datepart (dw,..
to filter data needed
This question already has an answer here:
Get the number of days between two dates in Oracle, inclusive of the dates
(1 answer)
Closed 8 years ago.
I want to calculate difference between two dates, something like:
SELECT TO_DATE('22-NOV-08')-TO_DATE('25-AUG-2008') FROM DUAL;
which comes out to be 89 is the TO_DATE('22-NOV-08') and TO_DATE('25-AUG-2008') included in this 89 days ?
To explain your query
SELECT TO_DATE('22-NOV-08')-TO_DATE('25-AUG-2008') FROM DUAL;
TO_DATE('22-NOV-08') converts the varchar value to date datatype and then what you are doing is nothing but enddate - startdate which will return the number of days elapsed.
In case you want the result in
1.hours -- multiply the result with 24
2.Minutes -- multiply the result with 24*60
so on ...
EDIT: if your question is; whether the result is inclusive of enddate and startdate then the answer is yes and so you have got the result as 89; else you would have got a result of 87 instead.
Yes, the difference is in units of days. Here is a tutorial for Date Arithmetic
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
90 days range using SQL server
I am trying to get the counts 90 days prior to the operational date and the counts 90 days after the operational date. For example, my operational date is 4/1/2004. So,90 days prior to 4/1/2004 is (1/2/2004 to 3/31/2004) and 90 days after (including 4/1/2004) is 6/29/2004.
I used the following scripts and mannually calculate the days, which is not efficient...
select
site,
count(*) as prior_counts
from mytable
where mydate >='1/2/2004'
and mydate <'4/1/2004'
group by site
select
site,
count(*) as after_counts
from mytable
where mydate >='4/1/2004'
and mydate <'6/30/2004'
group by site
You should look at the DATEDIFF function or equivalent if you're not using SQL Server.
DATEDIFF on MSDN
If you are passing the date parameter in from an application, consider modifying the application to do the date range calculation for you. By passing in two parameters, you are taking the burden of the calculation off SQL, which will improve performance.