Skip to content
Advertisement

Easter / Good Friday for Sowflake Date Dim

Further Evolved : The code below satisfies what was wanted in earlier comments [if you follow history] :), my new question right below is stumping me.

Question: I want to have ‘Week_Of_Year’ Adjust every Sunday, the code below does it every Monday.. I have tried 1 and 0 but via alter session per Snowflake but no luck! Any idea how to have a new week triggered by Sunday not Monday (as that is what it is currently)?

ie the result I want is for Date:1/2/2005 to reflect Week_Of_Year as 2 , not 1. alter session set week_of_year_policy = 1; vs alter session set week_of_year_policy = 0;
WORKING CODE

Evolved Question: Please view updated code, where I translated the T-SQL logic to Calculate Easter into something Snowflake could understand. The only issue is that for Year 2005 I am off by one day ( the code below returns March 28, 2005 for Easter but it is March 27, 2008).

Question: Can someone help me understand why the below line is so close but wrong, I have tried so many variations of rtrim, RIGHT, and other manipulations to the values that feed the date thinking it had to do with rounding down, but all making the return easter dates further off. Below is my closest for a 20 year span , all within about 1-5 days of actual.

Line that is causing error:date_from_parts(YR, '0' + rtrim(EasterMonth),'0' + rtrim(EasterDay)) AS test6

Easter Logic

Full Script:

Old Question: I have this nice piece of code for snowflake users that I need a little help finishing. I specifically want to use the second chunk of code that was written for SqlServer to be used in Snowflakes env, and integrated into my script below (first chunk of code).

Specifically: a)”How do you integrate a function into a query like this” as in Advice where to put the code inside my script above because I am having trouble understanding how to integrate a function within a select statement b)”Is there anything glaring about this query that would make running it in Snowflake uniquely difficult” I attempted to run the SQLServer “easter date” code alone inside snowflake, and changed the variables to match snowflakes requirements (ie take out @) and then I got an error unexpected 'BEGIN'.

Below is the SQLServer code I need help to input the above!! (thanks Function to return date of Easter for the given year)

Advertisement

Answer

New answer: You must use month and day numerical values directly, not reformat as TEXT:

Old answer:
It should be fairly simple to convert your T-SQL function to a Snowflake JavaScript function.
Maybe you have to learn JavaScript on the way, though.

The skeleton of such a function can be:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement