Night Shift Attendance in Oracle: Handling Punches That Cross Midnight (PL/SQL Guide)
How to calculate night-shift attendance in Oracle Database: shift windows, assigning punches that cross midnight to the right shift date, removing duplicate scans, missing OUT punches, overlap checks and month-end rules.
Oracle PL/SQL · HRMS
Night Shift Attendance in Oracle: Handling Punches That Cross Midnight
In a textile mill the night shift is where attendance software usually breaks. A worker punches in at 19:55 on Monday and punches out at 08:05 on Tuesday. A system that groups punches by calendar date sees two half-days: Monday with an IN and no OUT, Tuesday with an OUT and no IN. The worker is marked absent twice, overtime is lost, and the payroll team spends the first week of every month correcting it by hand.
This guide shows how to model shifts and assign every punch to the correct shift date in Oracle Database, so that night shifts, rotating shifts and missing punches are handled by the system instead of by spreadsheets. The examples use plain SQL and PL/SQL and work on Oracle 12c and later, including 19c.
1. The core idea: attendance belongs to a shift date, not a calendar date
Every punch must be answered with one question: which shift instance does this punch belong to? A shift instance is a shift on a specific date – for example “Night shift of 3 March”, which starts at 20:00 on 3 March and ends at 08:00 on 4 March. The calendar date of the OUT punch (4 March) is irrelevant; the attendance record is for 3 March.
Once you think in shift instances, the rest becomes simple: build the expected shift windows for each employee, then place each raw punch into the window it falls in.
2. Tables you need
Keep raw machine data separate from processed attendance. Raw punches are evidence and should never be updated; processed attendance can be rebuilt at any time.
CREATE TABLE att_raw_punch (
punch_id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
emp_id NUMBER NOT NULL,
punch_time DATE NOT NULL,
device_id VARCHAR2(30),
loaded_on DATE DEFAULT SYSDATE
);
CREATE TABLE att_shift (
shift_code VARCHAR2(10) PRIMARY KEY,
start_min NUMBER NOT NULL, -- minutes after midnight, 20:00 = 1200
duration_min NUMBER NOT NULL, -- 12 hours = 720
early_in_min NUMBER DEFAULT 120, -- accept IN up to 2h before start
late_out_min NUMBER DEFAULT 240, -- accept OUT up to 4h after end
grace_min NUMBER DEFAULT 10
);
CREATE TABLE att_roster (
emp_id NUMBER NOT NULL,
shift_date DATE NOT NULL,
shift_code VARCHAR2(10) NOT NULL REFERENCES att_shift,
CONSTRAINT att_roster_pk PRIMARY KEY (emp_id, shift_date)
);
Storing the start as minutes after midnight and the length as a duration – instead of a start time and an end time – removes the classic bug where an end time of 08:00 is “earlier” than a start time of 20:00. The end is always start + duration, whether or not it crosses midnight.
The roster says which shift each employee works on each date. For fixed shifts you can generate it from the employee master; for rotating shifts (A/B/C weeks) it is generated from the rotation pattern every month.
3. Build the shift windows
For each roster row, calculate the window in which punches are accepted. A punch slightly before the shift start or a few hours after the end still belongs to that shift.
CREATE OR REPLACE VIEW att_shift_window_v AS
SELECT r.emp_id,
r.shift_date,
r.shift_code,
r.shift_date + s.start_min/1440 AS shift_start,
r.shift_date + (s.start_min + s.duration_min)/1440 AS shift_end,
r.shift_date + (s.start_min - s.early_in_min)/1440 AS window_start,
r.shift_date + (s.start_min + s.duration_min
+ s.late_out_min)/1440 AS window_end,
s.grace_min
FROM att_roster r
JOIN att_shift s ON s.shift_code = r.shift_code;
For a night shift starting at 20:00 on 3 March with a 12-hour duration, shift_end is 08:00 on 4 March, and the acceptance window runs from 18:00 on 3 March to 12:00 on 4 March.
Make sure windows of consecutive days do not overlap. If an employee works night shift on 3 March and day shift on 4 March, a 08:05 punch could fall in both windows. Keep the early-in and late-out allowances small enough that the windows never touch for any pair of shifts you actually roster, and add a check (see section 7) that reports overlaps before processing.
4. Assign punches to shifts
Now each punch is joined to the window it falls in. The first punch in the window is the IN, the last is the OUT.
CREATE OR REPLACE VIEW att_shift_punch_v AS
SELECT w.emp_id,
w.shift_date,
w.shift_code,
w.shift_start,
w.shift_end,
w.grace_min,
MIN(p.punch_time) AS first_in,
MAX(p.punch_time) AS last_out,
COUNT(p.punch_id) AS punch_count
FROM att_shift_window_v w
LEFT JOIN att_clean_punch_v p
ON p.emp_id = w.emp_id
AND p.punch_time BETWEEN w.window_start AND w.window_end
GROUP BY w.emp_id, w.shift_date, w.shift_code,
w.shift_start, w.shift_end, w.grace_min;
Because the join is on the window and not on TRUNC(punch_time), the 08:05 punch on 4 March lands in the night shift of 3 March automatically. The view reads from att_clean_punch_v, which is described next.
5. Remove duplicate punches
Workers often press their finger twice, or a face terminal records the same person two or three times in a few seconds. If the only punches are two scans at 19:55 and 19:56, MIN and MAX will report a 1-minute shift. Filter near-duplicates before grouping, using LAG:
CREATE OR REPLACE VIEW att_clean_punch_v AS
SELECT punch_id, emp_id, punch_time
FROM (SELECT p.*,
LAG(p.punch_time) OVER (PARTITION BY p.emp_id
ORDER BY p.punch_time) AS prev_time
FROM att_raw_punch p)
WHERE prev_time IS NULL
OR punch_time - prev_time > 2/1440; -- ignore repeats within 2 minutes
Create this view before att_shift_punch_v. Then add a rule: if punch_count = 1, or the gap between first and last punch is shorter than, say, 60 minutes, the OUT is missing.
6. Classify the day
With IN and OUT in place, the status calculation is a straightforward CASE:
SELECT emp_id, shift_date, shift_code, first_in, last_out,
CASE
WHEN punch_count = 0 THEN 'ABSENT'
WHEN punch_count = 1
OR (last_out - first_in)*1440 < 60 THEN 'MISSING_OUT'
WHEN first_in > shift_start + grace_min/1440 THEN 'LATE'
ELSE 'PRESENT'
END AS day_status,
ROUND(GREATEST(0, (first_in - shift_start)*1440)) AS late_min,
ROUND(GREATEST(0, (last_out - shift_end)*1440)) AS overtime_min,
ROUND((last_out - first_in)*1440) AS worked_min
FROM att_shift_punch_v;
Two practical notes from mill payrolls:
- Do not auto-fill missing OUT punches. Show them on an exception screen for supervisors to resolve. Auto-filling with the shift end hides machine faults and silently pays hours that may not have been worked.
- Calculate overtime from the scheduled end, not from the IN time. A worker who arrives 40 minutes early has not earned 40 minutes of overtime unless your policy says so. Keep early-arrival credit as a separate, policy-controlled rule.
7. Checks to run before every processing cycle
Most attendance disputes come from bad setup, not bad code. Run these checks before calculating a month:
- Overlapping windows: for each employee, compare each window’s
window_endwith the next row’swindow_startusingLEAD. Any overlap must be fixed in the roster or the shift allowances. - Punches that belong to no window: punches outside every window usually mean the roster is missing (a weekly off that was worked) or the employee changed shift without the roster being updated.
- Employees with punches but no roster: new joiners are the usual cause.
- Machine clock drift: compare each device’s latest punch time with the database time. A device whose clock is ten minutes slow will produce a wave of “late” marks.
-- Overlapping windows per employee
SELECT emp_id, shift_date, window_end, next_start
FROM (SELECT emp_id, shift_date, window_end,
LEAD(window_start) OVER (PARTITION BY emp_id
ORDER BY shift_date) AS next_start
FROM att_shift_window_v)
WHERE next_start < window_end;
8. Month-end: which month does the last night shift belong to?
The night shift that starts at 20:00 on the 31st ends on the 1st of the next month. With shift-date logic it belongs to the 31st, so it is paid in the month it started. Agree this rule with HR and finance before go-live and write it in the payroll policy – it is the most common question at month end, and the answer must be the same every month.
The same principle applies to Ramadan timings or temporary shift changes: change the roster (which shift code applies on which date), not the punches. Raw data stays untouched; the calculation follows the roster.
9. Store the result, keep it rebuildable
Write the classified rows into an att_daily table that payroll reads. Keep a processed_on column and reprocess a date range by deleting and re-inserting rows from the views – never by editing raw punches. Manual corrections (for example, a supervisor approving a missing OUT) belong in a separate adjustments table that the processing step applies on top, with the user name and reason recorded. This way you can always explain to an auditor, or to a worker, exactly why a day was marked the way it was.
Quick checklist
- Shifts stored as start minutes plus duration, not start and end times
- A roster row for every employee for every working date
- Punches assigned by shift window, never by
TRUNC(punch_time) - Duplicate scans removed before IN/OUT is chosen
- Missing OUT punches shown as exceptions, not auto-filled
- Overlap, unrostered-punch and clock-drift checks before each run
- Written month-end rule for night shifts crossing into the next month
Related guides: connecting ZKTeco machines to Oracle Database, the HRMS implementation checklist for textile mills, designing a payroll engine in PL/SQL and the payroll software features checklist.
Need help with your project?
AbairTech builds Oracle APEX, ERP/POS, HRMS and business software. Explore our live demos or discuss your requirement.
Related implementation services
Need help applying these concepts in a business system? Explore Oracle APEX application development or Database and PL/SQL services.