[英]How to set field2 = weekday +1
I'm looking for code that will automatically enter in the next weekday's date into Field2 when Field1 is not null. 我正在寻找当Field1不为null时将在下一个工作日的日期自动输入到Field2中的代码。 Or more precisely Field2 = Field1 + 1 weekday
或更准确地说,Field2 = Field1 + 1个工作日
I am using Access 2007 as a front End and SQL 2008 as a backend. 我将Access 2007用作前端,将SQL 2008用作后端。
Any help would be greatly appreciated! 任何帮助将不胜感激!
Sounds like you're looking for an UPDATE
statement with DATEADD
: 听起来您正在寻找带有
DATEADD
的UPDATE
语句:
update YourTable
set Field2 = dateadd(day,1,Field1)
where Field1 is not null
Or if you are trying to remove weekends and always update to the nearest Monday, then this is one way with CASE
: 或者,如果您尝试删除周末并始终更新到最近的星期一,那么这是
CASE
一种方法:
update YourTable
set Field2 =
case
when datepart(dw,Field1) = 1 then dateadd(day,1,Field1)
when datepart(dw,Field1) = 7 then dateadd(day,2,Field1)
else dateadd(day,1,Field1)
end
where Field1 is not null
If you will be running a query from within Access against a linked SQL Server table, the query must use Access-compatible SQL. 如果要在Access中针对链接的SQL Server表运行查询,则该查询必须使用与Access兼容的SQL。
To make Field2
one day later than the Field1
date: 要使
Field2
比Field1
:
UPDATE YourTable
SET Field2 = DateAdd('d', 1, Field1)
WHERE Field1 Is Not Null;
To skip over weekend days, use the Weekday
function in a Switch
statement to determine how many days to add. 要跳过周末,请在
Switch
语句中使用Weekday
函数来确定要添加的天数。
UPDATE YourTable
SET Field2 =
DateAdd
(
'd',
Switch
(
Weekday(Field1) = 6, 3,
Weekday(Field1) = 7, 2,
Weekday(Field1) >= 1 AND Weekday(Field1)<=5, 1
),
Field1
)
WHERE Field1 Is Not Null;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.