SQL 本周和上周的第一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1150251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
First day Of this week and last week
提问by George
I am currently getting first day Of this week and last week values with vbscript function in 2/12/2009 format. I was wondering if it was possible with SQL query.
我目前正在使用 2/12/2009 格式的 vbscript 函数获取本周的第一天和上周的值。我想知道是否可以使用 SQL 查询。
回答by Ben Griswold
These statements should do what you want in TSQL. Note, the statements are based on the current date. You can replace getdate() for whatever date you wish:
这些语句应该在 TSQL 中执行您想要的操作。请注意,这些报表基于当前日期。您可以将 getdate() 替换为您希望的任何日期:
Select dateadd(wk, datediff(wk, 0, getdate()) - 1, 0) as LastWeekStart
Select dateadd(wk, datediff(wk, 0, getdate()), 0) as ThisWeekStart
Select dateadd(wk, datediff(wk, 0, getdate()) + 1, 0) as NextWeekStart
There are lots of other date routines here.
这里还有很多其他约会惯例。
回答by George
Ben's answer gives the wrong answer if today is Sunday. Regardless of whether the first day of the week is Sunday or Monday, the current date should be included in the current week. When I run his code for 3/24/2013, it gives me a ThisWeekStart of 3/25/2013. I used the following code instead: SELECT DATEADD(DAY, 1 - DATEPART(DW, '3/24/2013'), '3/24/2013')
如果今天是星期天,Ben 的回答给出了错误的答案。无论一周的第一天是星期日还是星期一,都应将当前日期包含在当前周中。当我在 2013 年 3 月 24 日运行他的代码时,它给了我 2013 年 3 月 25 日的 ThisWeekStart。我改用以下代码: SELECT DATEADD(DAY, 1 - DATEPART(DW, '3/24/2013'), '3/24/2013')
回答by mohitkumarmodi
Get the first day of current week.
for extra information use this link.
获取本周的第一天。
如需更多信息,请使用此链接。
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_dayofweek
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_dayofweek
select date(curdate()-DAYOFWEEK(curdate()-1)) as dat;