Sql server 获取从周一到周日的当前工作日
我试过解决这个问题的方法Sql server 获取从周一到周日的当前工作日,sql-server,sql-server-2008,Sql Server,Sql Server 2008,我试过解决这个问题的方法 select dateadd(wk, datediff(wk, 0, getdate()), 0)as StartDate , (select dateadd(wk, datediff(wk, 0, getdate()), 0) + 5) as EndDate 结果是星期一到星期六,但星期天给我下星期的时间 我希望星期天是一周的最后一天,星期一是一周的第一天 请帮助…通常,使用SET DATEFIRST 1指定星期一是一周的第一天。然而,这并不能解决这里的问题。
select dateadd(wk, datediff(wk, 0, getdate()), 0)as StartDate ,
(select dateadd(wk, datediff(wk, 0, getdate()), 0) + 5) as EndDate
结果是星期一到星期六,但星期天给我下星期的时间
我希望星期天是一周的最后一天,星期一是一周的第一天
请帮助…通常,使用SET DATEFIRST 1指定星期一是一周的第一天。然而,这并不能解决这里的问题。请改用以下语法:
SELECT DATEADD(week, DATEDIFF(day, 0, getdate())/7, 0) AS StartWeek,
DATEADD(week, DATEDIFF(day, 0, getdate())/7, 5) AS EndWeek
你只需增加6天,而不是5天
select dateadd(wk, datediff(wk, 0, getdate()), 0) as StartDate
select dateadd(wk, datediff(wk, 0, getdate()), 0) + 6) as EndDate
这可能过于复杂,但却充满了乐趣 -第一部分是获取最近发生的星期一 -它首先创建一个表,将所有日期保存到最近的星期一,然后将该表的最小值设置为@mondaythisweek变量
declare @dateholder table (
thedate date,
theday varchar(10)
)
declare @now datetime
set @now = GETDATE()
;with mycte as (
select
cast(@now as date) as "thedate",
DATENAME(dw,@now) as "theday"
union all
select
cast(DATEADD(d,-1,"thedate") as date) as "thedate",
DATENAME(DW,DATEADD(d,-1,"thedate")) as "theday"
from
mycte
where
"theday" <> 'Monday'
)
insert into @dateholder
select * from mycte
option (maxrecursion 10)
declare @mondaythisweek date
set @mondaythisweek = (
select min(thedate)
from @dateholder
)
-此部分创建从@mondaythisweek到下一个星期日的表
;with mon_to_sun as (
select
@mondaythisweek as "dates",
DATENAME(dw,@mondaythisweek) as "theday"
union all
select
cast(DATEADD(d,1,"dates") as date) as "dates",
DATENAME(dw,cast(DATEADD(d,1,"dates") as date)) as "theday"
from mon_to_sun
where "theday" <> 'Sunday'
)
select *
from mon_to_sun
option(maxrecursion 10)
随着时间的推移
你需要看看这个答案,非常感谢,我从中得到了所需的解决方案。完美的答案。谢谢你没有工作!!!!!它只显示从周一到今天的几天,而不是所有的工作日week@csharp_devloper31你可以阅读他的评论,他的方法,他接受了。所以我想我理解对了。但若你们想要一周中的所有日子,我相信还有另一个关于stackoverflow的问题可以回答。如果没有,请随意询问。最好的方法是创建一个日历表,如果你还没有它。
declare @dateholder table (
thedate date,
theday varchar(10)
)
declare @now datetime
set @now = GETDATE()
;with mycte as (
select
cast(@now as date) as "thedate",
DATENAME(dw,@now) as "theday"
union all
select
cast(DATEADD(d,-1,"thedate") as date) as "thedate",
DATENAME(DW,DATEADD(d,-1,"thedate")) as "theday"
from
mycte
where
"theday" <> 'Monday'
)
insert into @dateholder
select * from mycte
option (maxrecursion 10)
declare @mondaythisweek date
set @mondaythisweek = (
select min(thedate)
from @dateholder
)
;with mon_to_sun as (
select
@mondaythisweek as "dates",
DATENAME(dw,@mondaythisweek) as "theday"
union all
select
cast(DATEADD(d,1,"dates") as date) as "dates",
DATENAME(dw,cast(DATEADD(d,1,"dates") as date)) as "theday"
from mon_to_sun
where "theday" <> 'Sunday'
)
select *
from mon_to_sun
option(maxrecursion 10)
SELECT DATEADD(week, DATEDIFF(day, 0, GETDATE())/7, 0) AS 'StartWeek(Monday)',
DATEADD(week, DATEDIFF(day, 0, GETDATE())/7, 6) AS 'EndWeek(Sunday)'
SELECT DATEADD(week, DATEDIFF(day, 0, GETDATE())/7, 0) AS 'StartWeek(Monday)',
DATEADD(DAY,DATEDIFF(day, 0, DATEADD(week, DATEDIFF(day, 0, GETDATE())/7, 6)), '23:59:59') AS 'EndWeek(Sunday)'