2013-10-08 28 views
8

Ho provato la soluzione per questo è come.Ottenere giorni di settimana correnti da lunedì a domenica

select dateadd(wk, datediff(wk, 0, getdate()), 0)as StartDate , 
    (select dateadd(wk, datediff(wk, 0, getdate()), 0) + 5) as EndDate 

dà lunedi-sabato nel risultato, ma la Domenica mi dà prossimi giorni feriali

voglio domenica come ultimo giorno della settimana e Lunedi come primo giorno della settimana ..

prega Aiuto ...

+0

È necessario dare uno sguardo a questa risposta [Get primo giorno della settimana in SQL Server] (http://stackoverflow.com/a/7169656/1297603) – Yaroslav

risposta

9

In generale, utilizzare SET DATEFIRST 1 per specificare che il lunedì è il primo giorno della settimana. Tuttavia, questo non risolve il problema qui. Utilizzare questa sintassi invece:

SELECT DATEADD(week, DATEDIFF(day, 0, getdate())/7, 0) AS StartWeek, 
     DATEADD(week, DATEDIFF(day, 0, getdate())/7, 5) AS EndWeek 

Demo

SET DATEFIRST (Transact-SQL)

1 Monday 
2 Tuesday 
3 Wednesday 
4 Thursday 
5 Friday 
6 Saturday 
7 (default, U.S. English) Sunday 
+0

grazie mille, ho ottenuto la soluzione richiesta da questo. – saylesh

+0

Risposta perfetta. Grazie –

1

Si aggiunge solo 6 giorni invece di 5.

select dateadd(wk, datediff(wk, 0, getdate()), 0) as StartDate 
select dateadd(wk, datediff(wk, 0, getdate()), 0) + 6) as EndDate 
+0

Errore: errore di sintassi errato –

0
DECLARE 
    @d datetime, 
    @f datetime; 

SET @d = dateadd(week,datediff(week,0,getdate())-48,0) --start of week from a year ago 
SET @f = dateadd(week,datediff(week,0,getdate()),0) --start of current partial week; 

create table #weeks (
    week_starting datetime primary key 
) 

while @d < @f 
begin 
    insert into #weeks (week_starting) values (@d) 
    set @d = dateadd(week,1,@d) 
end 
select * from #weeks 

drop table #weeks 
0

Questo potrebbe essere eccessivamente complicato, ma è stato divertente.

- Questa prima parte è per ottenere l'ultimo lunedì.

- Inizia creando una tabella che manterrà tutte le date fino al lunedì più recente, quindi imposta il minimo di tale tabella sulla variabile @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 
) 

--Questo parte crea una tabella da @mondaythisweek alla prossima domenica

;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) 
Problemi correlati