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

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.

Related

Trying to accommodate relative defined date, such as 5 days ago, into my fixed date condition in PostgreSQL

I'm trying to condition my WHERE clause to accommodate relatively defined dates into my date filter. I'm pretty confused what type I need to use, if it's CONVERT or TO_DATE function, or if I need to put a CASE WHEN statement into my code.
This is the code that I have written so far:
WHERE event_create_verified_user_success.created_at_utc_date
BETWEEN DATE '2021-11-29' AND DATE '2021-12-05'
And this is the condition of the activity I need to finish:
If the desired date-period is not set manually using fixed dates like from “2021-11-29”
to “2021-12-05”, how would you change the where-clause to consider all data from relative
defined dates: “consider messages created between 10 days and 5 days ago (inclusive)”
I've only started PostgreSQL yesterday and the last time I've handle SQL was probably 4 years ago so I'm pretty confused at how much SQL has changed since then.
Thank you so much for helping!
The basic syntax hasn't really changed in the last 4 years (or even 15 years).
You can use current_date to obtain "today's date". You can subtract days from that
where ... between current_date - 10 and current_date - 5
If created_at_utc_date is a timestamp (= date and time) rather than a date (=date without time) it's better to use a range query though:
where created_at_utc_date >= current_date - 10
and created_at_utc_date < current_date - 4
Note the < combined with the next day you want to compare with.

How to fix a datetime restriction problem in a sql query?

I'm developing a SQL Query, as a restriction, I want that the query returns data from the first day of the last month until now.
For example: If the query is executed today, the query will only return data entered from 01/03/2019 until today.
To do this, i created the following restriction:
...
SF1010.F1_DTDIGIT>=concat(YEAR(GETDATE()),'0',month(GETDATE())-1,'01') AND
...
The problem is in the months 01, 11 and 12:
If the month is "11", the algorithm will return "1901001" and not "191001".
If the month is "01", the algorithm will return "190001" and not "181201".
Thanks for any help.
SF1010.F1_DTDIGT BETWEEN $yourDate AND date_add($yourDate,interval -DAY($yourDate)+1 DAY)
This gets dates between your date and the first day of the month. first day logic works by subtracting the DAY part to get first of month.
This query will do the comparison of your DTDIGIT with the first day of the previous month in the DATE format:
SF1010.F1_DTDIGIT >= STR_TO_DATE(CONCAT(YEAR(DATE_SUB(NOW(), INTERVAL 1 MONTH)),'-',MONTH(DATE_SUB(NOW(), INTERVAL 1 MONTH)),'-','1'), '%Y-%m-%d')
Explanation
DATE_SUB(NOW(), INTERVAL 1 MONTH) returns the date exactly one month ago. We use that date to get the previous month
YEAR() and MONTH() functions use the date explained above
STR_TO_DATE() function converts a string representing a date to the
MySQL DATE format, using the mask (in this example, it's
'%Y-%m-%d').
How about this?
SF1010.F1_DTDIGT >= date_add(date_add(curdate(), interval 1 - day(curdate()) day), interval -1 month)
This assumes the date is not in the future. You can add another condition if that is possible.
If you happen to be using SQL Server, you can do this weird form of date arithmetic:
SF1010.F1_DTDIGT >= dateadd(month, datediff(month, 0, getdate()) - 1, 0)

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.

Difference between two dates in sql [duplicate]

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

SQL Query data range limit

I have this where SQL Query. I want to know if I can limit that user can only pick 2 days of of date range. meaning they cannot pick 1 month or 1 week or more than 2 days difference of querty data range.
WHERE
To_Date(to_char(B.time_stamp, 'DD-MON-YYYY')) >= To_Date('?DATE1::?','MM/DD/YYYY')
and To_Date(to_char(B.rest_date, 'DD-MON-YYYY')) <= To_Date('?DATE2::?','MM/DD/YYYY')
If I understand your question, you are just asking how to limit the two dates so that they are within 2 days of each other. Date math is pretty simple in Oracle (which is my guess as to the DB you are using):
WHERE ABS(Date1 - Date2) <= 2
You don't need to convert it at all with to_char or anything else, since it is stored internally as an actual date. You can use this same type of logic to make sure it is less than 16 hours:
WHERE ABS(Date1 - Date2) <= 16/24
As long as you remember to adjust your units appropriately.
Note that in this case, 2 days means 48 hours. If you mean it has to be 2 actual days, then it is slightly different.