Logic to ensure SMS is sent only between 10:00 AM and 8:00 PM

production-safe logic to ensure SMS is sent only between 10:00 AM and 8:00 PM.


✅ Simple & reliable Oracle logic (recommended)

AND TO_NUMBER(TO_CHAR(SYSDATE, 'HH24')) BETWEEN 10 AND 19

✔ Why 19 and not 20?

  • HH24 = 20 means 8:00–8:59 PM

  • “Between 10 AM and 8 PM” usually means up to 7:59 PM

  • So valid hours: 10–19


⏰ Exact time control (10:00 to 20:00 sharp)

If you want exactly up to 8:00 PM (20:00:00):

AND SYSDATE BETWEEN
    TRUNC(SYSDATE) + 10/24
AND TRUNC(SYSDATE) + 20/24

✔ Covers:

  • 10:00:00 AM ✅

  • 07:59:59 PM ✅

  • 08:00:00 PM ✅


🚫 Block SMS outside time window (PL/SQL style)

IF SYSDATE NOT BETWEEN
     TRUNC(SYSDATE) + 10/24
 AND TRUNC(SYSDATE) + 20/24
THEN
   RETURN; -- do not send SMS
END IF;

🧠 Best practice 

Use time window + day logic together:

AND TO_CHAR(SYSDATE,'DY','NLS_DATE_LANGUAGE=ENGLISH') <> 'SUN'
AND SYSDATE BETWEEN
    TRUNC(SYSDATE) + 10/24
AND TRUNC(SYSDATE) + 20/24

✔ No SMS on Sunday
✔ Only 10 AM – 8 PM


🔥 Pro tip (for scheduler jobs)

If this runs as a job every few minutes, don’t fail the job—just skip SMS:

IF SYSDATE < TRUNC(SYSDATE) + 10/24
OR SYSDATE > TRUNC(SYSDATE) + 20/24
THEN
   DBMS_OUTPUT.PUT_LINE('Outside SMS window');
   RETURN;
END IF;

PROCEDURE send_sms_proc (p_mobile VARCHAR2, p_msg VARCHAR2) IS

BEGIN

  -- Allow SMS only between 10:00 AM and 8:00 PM

  IF SYSDATE NOT BETWEEN

       TRUNC(SYSDATE) + 10/24

   AND TRUNC(SYSDATE) + 20/24

  THEN

     -- Outside allowed window, skip SMS

     RETURN;

  END IF;


  -- Optional: Exclude Sunday

  IF TO_CHAR(SYSDATE, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') = 'SUN' THEN

     RETURN;

  END IF;


  -- Actual SMS send logic  you need to configure SMSC then write logic for the following 

  send_sms(p_mobile, p_msg);

END send_sms_proc;


Post a Comment

And that's all there is to it!

If anyone has any other questions or requests for future How To posts, you can either ask them in the comments or email me. Please don't feel shy at all!

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

Previous Post Next Post