How to get Previous business day in a week with that of current Business Day using sql server

后端 未结 8 2074
情歌与酒
情歌与酒 2020-12-14 09:00

i have an ssis Package which runs on business days (mon-Fri). if i receive file on tuesday , background(DB), it takes previous business day date and does some transactions.

8条回答
  •  时光说笑
    2020-12-14 09:55

    The simplest solution to find the previous business day is to use a calendar table with a column called IsBusinessDay or something similar. The your query is something like this:

    select max(BaseDate)
    from dbo.Calendar c
    where c.IsBusinessDay = 0x1 and c.BaseDate < @InputDate
    

    The problem with using functions is that when (not if) you have to create exceptions for any reason (national holidays etc.) the code quickly becomes unmaintainable; with the table, you just UPDATE a single value. A table also makes it much easier to answer questions like "how many business days are there between dates X and Y", which are quite common in reporting tasks.

提交回复
热议问题