How do I go about getting the first date of current year using Redshift?
I am familiar with T-SQL/SQL in SSMS but I am not entirely sure how to do this in Redshift.
In SSMS I would have done:
SELECT
DATEADD(yy, DATEDIFF(yy, 0, GETDATE()), 0) AS StartOfYear
However, some of the functions were not working using Amazon Redshift.
DATE_TRUNC
with year
param will do this.
SELECT DATE_TRUNC('year', TIMESTAMP '20200430 04:05:06.789');
OUTPUT
date_trunc |
---|
2020-01-01 00:00:00 |
Just in case someone needs it dynamically without explicitly specifying the date it can be done this way:
CAST(DATE_TRUNC('year', current_date) AS DATE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With