I found that there is a function last_day
for last day of month, and date_part(dow, date)
for numeric day of week starting with Sunday, but I am trying to take a date, and get the first day of that week.
我发现每个月的最后一天有一个函数last_day,而date_part(dow,date)表示从星期日开始的数字星期几,但是我想要一个约会,并获得该周的第一天。
Meaning: if date='2018-02-14
' then result should be '2018-02-11
'.
含义:如果date ='2018-02-14',则结果应为'2018-02-11'。
Any ideas?
有任何想法吗?
1 个解决方案
#1
3
You simply want to subtract the dow
value from the current date.
您只想从当前日期中减去道具值。
select dateadd(d, -datepart(dow, my_date), my_date)
from (select date('2018-02-14') as my_date)
> 2018-02-11 00:00:00.0
For example, if dow
is 3 for 2018-02-14
- a Wednesday - you can subtract 3 days to get back to "day 0".
例如,如果2018-02-14(周三)的道绩为3,则可以减去3天以回到“第0天”。
There's also the date_trunc
function which will truncate everything after a given datepart. This is a little clunky, and will only set you back to the previous Monday, not Sunday.
还有date_trunc函数,它会在给定的datepart之后截断所有内容。这有点笨重,只会让你回到上周一,而不是周日。
select date_trunc('week', my_date)
from (select date('2018-02-14') as my_date)
#1
3
You simply want to subtract the dow
value from the current date.
您只想从当前日期中减去道具值。
select dateadd(d, -datepart(dow, my_date), my_date)
from (select date('2018-02-14') as my_date)
> 2018-02-11 00:00:00.0
For example, if dow
is 3 for 2018-02-14
- a Wednesday - you can subtract 3 days to get back to "day 0".
例如,如果2018-02-14(周三)的道绩为3,则可以减去3天以回到“第0天”。
There's also the date_trunc
function which will truncate everything after a given datepart. This is a little clunky, and will only set you back to the previous Monday, not Sunday.
还有date_trunc函数,它会在给定的datepart之后截断所有内容。这有点笨重,只会让你回到上周一,而不是周日。
select date_trunc('week', my_date)
from (select date('2018-02-14') as my_date)