Oracle
 sql >> Database >  >> RDS >> Oracle

Il prossimo giorno lavorativo (lunedì - venerdì) in Oracle?

Queste cose vengono normalmente eseguite con una tabella che elenca tutti i giorni lavorativi in ​​un anno piuttosto che una formula ingenua come:

CASE WHEN (1 + TRUNC (SYSDATE) - TRUNC (SYSDATE, 'IW')) < 5 
  THEN 1 + (1 + TRUNC (SYSDATE) - TRUNC (SYSDATE, 'IW')) 
  ELSE 1 
END --(on monday to thursday this returns 2 to 5, on fri/sat/sun this returns 1)

CASE WHEN (1 + TRUNC (SYSDATE) - TRUNC (SYSDATE, 'IW')) < 5 
  THEN TRUNC (SYSDATE) + 1 
  ELSE TRUNC (SYSDATE + 4), 'IW')  
END --(on monday to thursday this returns tomorrow's date, on fri/sat/sun it returns next monday's date)

L'uso di una tabella consente di tenere conto di cose come festività, festività nazionali, feste religiose, giorni di votazione, giorni di lutto, giorni liberi ad hoc che possono verificarsi per motivi aziendali o politici, ecc. La manutenzione della tabella diventa quindi un compito importante. Il recupero del giorno lavorativo successivo è essenzialmente un caso di interrogazione della tabella per il MIN(date) WHERE date > current_date