Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generate dates within a range in Snowflake

I have a table in postgres
one of the columns is year which is calculated as :

date_part('year',current_date) + generate_series(-2,4)

so if I take today’s date , the output will be :

year
2021
2025
2019
2024
2023
2022
2020

How to achieve the above in snowflake environment.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance

>Solution :

Using GENERATOR and DATEADD:

SELECT DATEADD(year, (ROW_NUMBER() OVER(ORDER BY seq8())-3, current_date) AS y,
FROM TABLE(GENERATOR(ROWCOUNT => 7));

And year only:

SELECT YEAR(CURRENT_DATE) + (ROW_NUMBER() OVER(ORDER BY seq8())-3 AS y,
FROM TABLE(GENERATOR(ROWCOUNT => 7));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading