简体   繁体   English

本周的第一天和上周

[英]First day Of this week and last week

I am currently getting first day Of this week and last week values with vbscript function in 2/12/2009 format. 我目前正在获得本周的第一天和上周以2月12日格式的vbscript函数值。 I was wondering if it was possible with SQL query. 我想知道是否可以使用SQL查询。

These statements should do what you want in TSQL. 这些语句应该在TSQL中执行您想要的操作。 Note, the statements are based on the current date. 请注意,这些陈述基于当前日期。 You can replace getdate() for whatever date you wish: 您可以将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 . 这里有很多其他日期例程

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. 当我运行2013年3月24日的代码时,它给了我2013年3月25日的ThisWeekStart。 I used the following code instead: SELECT DATEADD(DAY, 1 - DATEPART(DW, '3/24/2013'), '3/24/2013') 我改为使用以下代码:SELECT DATEADD(DAY,1 - DATEPART(DW,'/ 3/24/2013'),'3/24/2013')

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM