Calculate diffference between 2 dates in SQL, excluding weekend days -


i build sql query calculates difference between 2 dates, without counting week-end days in result.

is there way format dates obtain result ? example oracle database :

select sysdate - creation_dttm the_table 

you should try function :

create function total_weekdays(date1 date, date2 date) returns int return abs(datediff(date2, date1)) + 1      - abs(datediff(adddate(date2, interval 1 - dayofweek(date2) day),                     adddate(date1, interval 1 - dayofweek(date1) day))) / 7 * 2      - (dayofweek(if(date1 < date2, date1, date2)) = 1)      - (dayofweek(if(date1 > date2, date1, date2)) = 7); 

test :

select total_weekdays('2013-08-03', '2013-08-21') weekdays1,        total_weekdays('2013-08-21', '2013-08-03') weekdays2; 

result :

| weekdays1 | weekdays2 | ------------------------- |        13 |        13 | 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -