Create a table in which I only have the dates with the business days of the year and I need to get the difference, in business days, between two dates.
The April business day table is:
SELECT FECHA
FROM AR_FECHA
WHERE MONTH(FECHA) = 4
The result is:
For example, how to know the difference in business days between today, April 17
SELECT GETDATE()
With the last date of the month of April (7 days).
Edition
After the comments, I have understood that what you want is to obtain the number of business days between two dates, based on your table of business days.
To do this, what you can do is simply, in a CTE, number the days in the table and then get the difference between that index of days between the two dates you are interested in.
For example, to get the difference between the current date and April 30, 2019, you could write a query like this:
Which, with your input data, would return this result:
Or you could generalize it to get the difference between a specific date and any other date in your table, for example this query:
It would return the following result:
original answer
You can use the function
DateDiff
to get the difference between two dates. The function can take the difference in years, quarters, months, days, hours, minutes, etc.To get the difference of today with all the dates in your table, do this query:
I don't know if it works for you but in sql sever 2008 to get the difference of days the DATEDIFF is used
Example:
I would return 1 day difference
According to your comment: select the last date of April and compare it with the current date.