Gasire zile weekend pe un interval

Configurare noua (How To)

Situatie

Pentru situatiile in care avem nevoie sa stim care sunt zilele de weekend intr-un interval de timp am facut o procedura care face acest lucru pe un interval de timp ales.
Procedura primeste doi parametri:
@begin de tip data
@end de tip data
Procedura verifica pt intervalul cuprins intre @begin si @end (inclusiv capetele de interval) si, daca gaseste, returneaza zilele de weekend.

Solutie

— exec BD_ZileWeekend_prc ‘20190706’ , ‘20190714’

alter procedure BD_ZileWeekend_prc
@begin DATE ,
@end DATE

as

DECLARE @Weekend TABLE
(
Weekend DATE PRIMARY KEY
,IsWeekend BIT
)

WHILE @begin <= @end

BEGIN

INSERT INTO @Weekend
SELECT
@begin AS Weekend ,
(CASE WHEN DATEPART(WEEKDAY, @begin) In (7, 1) THEN 1 ELSE 0 END) AS IsWeekend
SET @begin = DateAdd(Day, 1, @begin)

END

SELECT Weekend FROM @Weekend WHERE IsWeekend = 1

Tip solutie

Permanent
Etichetare:

Voteaza

(30 din 60 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?