Home > Software design >  Stored procedure : return first five days of the week
Stored procedure : return first five days of the week

Time:04-20

I would like to pass a single date to my stored procedure this could be any date:

2022-04-18

How would I be able to return the next 4 days of the week if possible to exclude weekends but its not necessary.

Expected output:

2022-04-18
2022-04-19
2022-04-20
2022-04-21
2022-04-22

CodePudding user response:

CREATE procedure spDateMessing(@d as date)
as
with cte as
(
    select n from (values(0),(1),(2),(3),(4)) as t(n)
)
select n,cast(DATEADD(D,n,@d) as date) as Date_ from cte

CodePudding user response:

Get a where condition and set the condition loop 5, inside the loop use DATEADD and for your FORMAT method like:

FORMAT(DATEADD(DAY, @cnting, '<start_date>'),'yyyy-mm-dd').
  • Related