[英]Subtract one week from current week of the year
I am trying to get one week earlier then current week of the year but my sql query is returning null. 我想提前一年,然后一年中的当前一周,但我的SQL查询返回null。 here is my query
这是我的查询
select date_sub(yearweek('2014-01-01'),INTERVAL 1 week)
what is wrong with this query 这个查询有什么问题
If you want to get YEARWEEK
of week prior to date, you can do this: Note: YEARWEEK
results in 6-digit number, first 4 digits are week year, trailing 2 digits are week number. 如果要获取日期之前的一周的
YEARWEEK
,可以执行以下操作:注意: YEARWEEK
以6位数字表示,前4位数字是星期年份,后2位是星期数字。
SELECT YEARWEEK('2014-01-01' - INTERVAL 1 WEEK)
If you need to get a date
that is one week before a given date, then: 如果你需要得到一个
date
是给定的日期,然后前一周:
SELECT '2014-01-01' - INTERVAL 1 WEEK
尝试这个
select date_sub(date('2014-01-01'),INTERVAL 1 week)
Try this:- 尝试这个:-
DATE_SUB(date('2014-01-01'), INTERVAL 7 DAY)
or 要么
SELECT '2014-01-01' - INTERVAL 1 WEEK
The problem is that DATE_SUB
takes a date as the first arguement, but year week returns yyyyww
ie not a date. 问题在于,
DATE_SUB
以日期为依据,但年份星期返回yyyyww
,而不是日期。 So this: 所以这:
SELECT yearweek('2014-01-01');
Returns 201352
, this is then implicitly casted to a date, since it is not a date the result is null. 返回
201352
,然后将其隐式转换为日期,因为它不是日期,结果为null。 You can replicate this by doing: 您可以这样做:
SELECT DATE(yearweek('2014-01-01'));
So if you subtract a week from NULL
the result is also NULL
. 因此,如果您从
NULL
减去一周,则结果也是NULL
。
The fix is to subtract the week first, then get the year week: 解决方法是先减去周,然后得到年周:
SELECT yearweek(date_sub('2014-01-01', INTERVAL 1 WEEK));
And the result is 201351
. 其结果是
201351
。
Are you looking for week number??? 您在寻找星期几吗??? If yes then plz try this if it will work for you
如果是的话,请尝试一下,如果它适合您
Select DatePart(Week, Date add(day,-7,convert(date time,'01-jan-2014')))
Pleas let me know if you are looking for something else. 请告诉我您是否正在寻找其他东西。
SELECT * FROM [table] WHERE WEEKOFYEAR(date) = WEEKOFYEAR(NOW()) - 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.