How to SET DATEFIRST equal to Sunday in Amazon Redshift

后端 未结 1 1219
悲哀的现实
悲哀的现实 2021-01-24 16:21

2016-01-01 is the week 1 of 2016, but also is the week 53 of 2015.

When I run SELECT DATE_PART(w, \'2016-01-01\') it returns 53, but when I run

相关标签:
1条回答
  • 2021-01-24 16:49

    Create a custom function with this case statement or use it in your SQL statements:

    case when DATE_PART('W', dt)>=51 and EXTRACT(DOY FROM dt) < 8 then 1 else DATE_PART('W', dt)+1 end week_no
    

    It will start counting weeks on Jan 1st.

    http://sqlfiddle.com/#!15/ccb90/9

    0 讨论(0)
提交回复
热议问题