# How to Calculate workdays in sql between assigned date and current date. Should exclude weekends.

I am using bigquery so the acceptable syntax is

datediff( CURRENT_DATE(),(timestamp(h.ARRIVAL_TIME_PAC_TZ))) as Aging_from_Arrival

calculates diffference between current and arrival date but it adds weekends too. Is there way to calculate only weekdays excluding weekends and holidays if possible.
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DATEDIFF("weekday",[Calculation1],[Calculation2])-2*(DATEPART('week', [Calculation2]) -DATEPART('week', [Calculation1]))

Calculation 1, 2 are the dates
Commented:
In oracle, I'd create a function to do the math

create or replace FUNCTION weekdays_between(p_start IN DATE, p_end IN DATE)
RETURN NUMBER
DETERMINISTIC
IS
v_start          DATE := TRUNC(LEAST(p_start, p_end));
v_end            DATE := TRUNC(GREATEST(p_start, p_end));

v_start_sunday   DATE := NEXT_DAY(v_start - 7, 'Sun');
v_end_sunday     DATE := NEXT_DAY(v_end - 7, 'Sun');
BEGIN
RETURN   v_end
- v_start
+ 1 -- all days
- (v_end_sunday - v_start_sunday) / 7 * 2 -- substract weekends
- CASE WHEN TO_CHAR(v_start, 'Dy') = 'Sun' THEN 1 ELSE 0 END -- subtract first day if it is Sunday
- CASE WHEN TO_CHAR(v_end, 'Dy') = 'Sat' THEN 1 ELSE 0 END -- substract last day if it is Saturday
;
END;

I use next_day instead of trunc to make it easier to transition between NLS settings, just change Sat and Sun to whatever the abbreviation is for the language needed.

Experts Exchange Solution brought to you by