本周的第一天和上周

时间:2022-10-21 10:14:41

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日格式的vbscript函数值。我想知道是否可以使用SQL查询。

3 个解决方案

#1


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.

这里有很多其他日期例程。

#2


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')

如果今天是星期天,本的回答给出了错误的答案。无论一周的第一天是星期日还是星期一,当前日期都应包含在当前星期。当我运行2013年3月24日的代码时,它给了我2013年3月25日的ThisWeekStart。我改为使用以下代码:SELECT DATEADD(DAY,1 - DATEPART(DW,'/ 3/24/2013'),'3/24/2013')

#3


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

select date(curdate()-DAYOFWEEK(curdate()-1)) as dat;

#1


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.

这里有很多其他日期例程。

#2


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')

如果今天是星期天,本的回答给出了错误的答案。无论一周的第一天是星期日还是星期一,当前日期都应包含在当前星期。当我运行2013年3月24日的代码时,它给了我2013年3月25日的ThisWeekStart。我改为使用以下代码:SELECT DATEADD(DAY,1 - DATEPART(DW,'/ 3/24/2013'),'3/24/2013')

#3


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

select date(curdate()-DAYOFWEEK(curdate()-1)) as dat;