'Snowflake - While Loop using Current_Date
In Snowflake I'm attempting to run a while loop where there is a start date, and the code will loop until it equals today's date.
I'm fairly new to snowflake itself.
First idea was in the while loop check if the variable date was less than the current date. I couldn't seem to get this working even with the Current_Date on it's own or attributed to a variable
execute immediate $$
declare
opdate := '2022-04-29';
currdate := Current_Date;
begin
while (opdate <=currdate) do
opdate :=dateadd(day,1,opdate);
end while;
end;
$$
;
'CURRDATE' cannot have its type inferred from initializer
The second option was to use a datediff to see if the difference between the variable and current_date was equal to zero
execute immediate $$
declare
opdate := '2022-04-29';
currndate := 1;
begin
while (currndate<=0) do
currndate := datediff(day, opdate, CURRENT_DATE);
opdate :=dateadd(day,1,opdate);
end while;
end;
$$
;
error line 7 at position 39 invalid identifier 'CURRENT_DATE'
I've tried many different variants of this code, even use for each and if loops with no luck. I'm seemingly unable to check if a variable is less than the current date. Any help/pointers would be much appreciated.
Without having Current_Date, and having a set start and end date, it appears to run fine
execute immediate $$
declare
opdate := '2022-04-29';
currdate := '2022-05-03';
begin
while (opdate <=currdate) do
opdate :=dateadd(day,1,opdate);
end while;
end;
$$
;
Solution 1:[1]
Can you try CURRENT_DATE() with the open and close parens? see block below
execute immediate $$
declare
opdate := '2022-04-29';
currdate := Current_Date();
begin
while (opdate <=currdate) do
opdate :=dateadd(day,1,opdate);
end while;
return opdate; --added this return
end;
$$
;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | P Needleman |
