Difference between two dates in sql [duplicate] - sql

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

Related

How to Subtract 30 days from current date in SQL? [duplicate]

This question already has answers here:
How to subtract 30 days from the current date using SQL Server
(4 answers)
Closed 8 months ago.
I am trying to subtract 30 days from a date column in my database and use that as a condition in my where but I can't get it to work
table example:
Fact_day
2022-05-20
2022-05-20
2022-04-15
2022-05-28
My trial:
where pr.fact_day between current_date and current_date - 30
Expected output is to get me all info in the rows that are 30 days before today's date
You can use DATEADD to subtract days to a given date
SQL Server DATEADD() Function
DATEADD(interval, number, date)
number Required. The number of interval to add to date. Can be positive (to get dates in the future) or negative (to get dates in the past)
Example:
SELECT DATEADD(day, -30, '2017/08/25') AS DateAdd;
https://www.w3schools.com/sql/func_sqlserver_dateadd.asp
Assuming Postgres, from Comparison operators:
The BETWEEN predicate simplifies range tests:
a BETWEEN x AND y
is equivalent to
a >= x AND a <= y
Notice that BETWEEN treats the endpoint values as included in the range. BETWEEN SYMMETRIC is like BETWEEN except there is no requirement that the argument to the left of AND be less than or equal to the argument on the right. If it is not, those two arguments are automatically swapped, so that a nonempty range is always implied.
So this:
between current_date and current_date - 30
needs to be this:
between current_date - 30 and current_date
unless you use SYMMETRIC.

Return the number of months between now and datetime value SQL

I apologize, I am new at SQL. I am using BigQuery. I have a field called "last_engaged_date", this field is a datetime value (2021-12-12 00:00:00 UTC). I am trying to perform a count on the number of records that were "engaged" 12 months ago, 18 months ago, and 24 months ago based on this field. At first, to make it simple for myself, I was just trying to get a count of the number of records per year, something like:
Select count(id), year(last_engaged_date) as last_engaged_year
from xyz
group by last_engaged_year
order by last_engaged_year asc
I know that there are a lot of things wrong with this query but primarily, BQ says that "Year" is not a valid function? Either way, What I really need is something like:
Date() - last_engaged_date = int(# of months)
count if <= 12 months as "12_months_count" (# of records where now - last engaged date is less than or equal to 12 months)
count if <= 18 months as "18_months_count"
count if <= 24 months as "24_months_count"
So that I have a count of how many records for each last_engaged_date period there are.
I hope this makes sense. Thank you so much for any ideas
[How to] Return the number of months between now and datetime value [in BigQuery] SQL
The simples way is just to use DATE_DIFF function as in below example
date_diff(current_date(), date(last_engaged_date), month)

Number of days between two columns SQL [duplicate]

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

Oracle SQL - Convert (Date and Julian Hours) to Julian Hours [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The dates are in gregorian date and the hours are in julian. How do I get the difference between in hours between the two times?
Assuming your date columns are of the date data type, and the time columns are of a string type (varchar2), you could do these calculations:
select to_char(trunc(hours) * 100 + (hours-trunc(hours))*60, 'fm0000') as hours_minutes
from (
select ( (dtrecv - dtcoll)
+ ( to_date(nvl(tmrecv, '0000'), 'hh24mi')
- to_date(nvl(tmcoll, '0000'), 'hh24mi'))
) * 24 as hours
from sample_demog_view
);
When time columns values are null, 0000 is assumed. When either one of the date columns is null the result will be null as well.
Explanation
Summary: the inner SQL calculates the number of hours between the two date/times as a fractional number. The outer query converts this number to the 4-digit 24-clock notation. NB: This second step might or might not be needed for you.
(dtrecv - dtcoll): the two time-less dates are subtracted from each other which gives a numerical value representing a number of days.
to_date(nvl(tmrecv, '0000'), 'hh24mi'): converts the 24-clock notation to a date/time, but the date part is zero. The same conversion is performed for tmcoll and it is subtracted from the first. This gives a numerical value representing a number of days, but it will be a fractional number between -1 and 1.
This gets added to the difference in days we already got, giving a fractional number, still expressing a number of days.
To translate that to number of hours: * 24.
The outer query then takes that value and truncates it to get the integer number of hours: trunc(hours)
Then the decimal part is taken: hours-trunc(hours). This is multiplied by 60 to get the corresponding number of minutes.
Finally, hours and minutes are added (hours multiplied by 100 to make room for the minutes) and formatted as a 4 digit string. Note that the difference may need more than 4 digits, so the to_char format might need to be modified if that is your case.
The reason for the outer query is that I wanted to avoid to repeat the same calculation, so I could just refer to it with hours. If there is a better solution that only refers to hours once, then there is no more need for this select nesting.

Hive - How do I get last months of data from Hive?

How do I get last 2 months of data from Hive?
Here is my attempt:
select (date_add(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'),
2 - month(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'))
));
This results in 2015-05-30. The results should be like: if Today is '2015-06-03', then the result of last two months should be like: '2015-04-01'. Notice that I put the first day of the month for the results. What am I doing wrong here? Thanks!
Extra Notes:
In SQL is it pretty easy to get:
select * from date_field >= DATEADD(MONTH, -2, GETDATE());
date_add adds days, not months. The below line evaluates to -4
2 - month(FROM_UNIXTIME(UNIX_TIMESTAMP(), 'yyyy-MM-dd'))
So you are basically subtracting 4 days from '2015-06-03', which is why you get the result '2015-05-30'.
As far as I know, there is no direct way to subtract months in Hive. Solutions you could consider:
Subtract 60 days, but that won't give you accurate results.
Write a custom UDF to return the date 2 months ago.
Calculate the date in a script, and pass it to hive.