Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first date of current year in Amazon Redshift

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.

like image 433
jcoke Avatar asked Oct 14 '25 11:10

jcoke


2 Answers

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
like image 83
Mehmet Güngören Avatar answered Oct 17 '25 00:10

Mehmet Güngören


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)
like image 24
jcoke Avatar answered Oct 17 '25 02:10

jcoke