unable to understand the dateadd function in SQL

I have a SQL query like

SET THIS_YEAR_END = '2022-11-01';
SET THIS_YEAR_START = DATEADD(DAY, -4*7+1, $THIS_YEAR_END);
SET LAST_YEAR_END = '2021-11-02';
SET LAST_YEAR_START = DATEADD(DAY, -4*7+1, $LAST_YEAR_END);

select end_date from (
select * from data
where DATE>= DATEADD(DAY, -27 * 7, $LAST_YEAR_START))
AND END_DATE BETWEEN CUST.END_DATE - 26 * 7 AND CUST.END_DATE - 7

I’m confused with this dateadd function in SQL. Can anyone please explain what exactly it’s doing?

>Solution :

Here is the docs: https://docs.snowflake.com/en/sql-reference/functions/dateadd.html

Basically DATEADD is adding a specified value to a certain date. The first parameter is indicating the units of time that you want to add (e.g. DAY or MONTH), the second parameter specifies the number of units (e.g. number of days or number of months) and the third paramter is the date, to which you want to add something.

In your example in line 2 you are reducing THIS_YEAR_END by -4*7+1 (=-27) days.

Leave a Reply