PS Query & SQL

 View Only
Expand all | Collapse all

How to account for months with more/less than 30 days using ADD_MONTHS function

  • 1.  How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 07-14-2025 05:49 PM

    Hi everyone!

    It is very possible that I am overthinking this one, but I am writing a query for a user that wants to see all active students who will be turning 21 years old within the next month, and they want the query to be scheduled to run on the 1st of every month. 

    In its current configuration, it will pull in students with birthdays coming up within the next 30 days (excluding those with birthdays on the date of the run), but is there a better way to do this that will account for months with less than 30 days (February, including leap years) and those with more (July, August, etc.)? 

    Since the report is going to be used as an email distribution list, it will duplicate some students (those with birthdays on March 2 would appear on both the February and March reports), and based on my testing, if it runs on the 1st of every month, it will miss anyone with a birthday on the 1st or 31st of the month. 

    Current criteria:

    Thank you in advance!

    Richard



    ------------------------------
    Richard Pope
    Senior Assistant Registrar for CU-SIS Development
    University of Colorado Colorado Springs
    ------------------------------

    Message from the HEUG Marketplace:
    ------------------------------
    Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
    ------------------------------
    Alliance 2026 Registration is Open!


  • 2.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 07-14-2025 07:45 PM
    Hi - 

    At a very quick thought.... what if you were to calculate their age (a fast google search and I selected this page which may be helpful: https://stackoverflow.com/questions/3015431/oracle-age-calculation-from-date-of-birth-and-today
    Hi @RaúlMoreno, that simple formula actually results in much lower precision. That article is about calculating an age in integer years only. Connor himself says "This is a reasonable approximation, but still...an approximation". My algorithm is much more accurate, and does not suffer from the original problem expressed in that link (dividing number of days by 365 will often be incorrect, for ...
    stackoverflow.com
    ... and then if the years = 20 and the month of the birthdate is the same as the month of the query run date and the day of the birthdate is greater than the date of the query run date (otherwise they just turned 20 this month I think) then they will be turning 21 this month.  That would seem to get you out of the number of days in a month.  

    Have fun!

    David Ehrlich | Senior Business System Analyst

    Duke University | Student Information Services & System (SISS)

    david.ehrlich@duke.edu | 919-684-1206

    www.sissoffice.duke.edu





    Alliance 2026 Registration is Open!


  • 3.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 07-15-2025 07:15 AM
    This SQL may work ... run on the first day of the month

    SELECT TO_CHAR(A.BIRTHDATE,'YYYY-MM-DD'), TRUNC(MONTHS_BETWEEN(SYSDATE, TO_CHAR(A.BIRTHDATE,'YYYY-MM-DD') ))/12
      FROM PS_PERSON A
      WHERE ( TO_CHAR( A.BIRTHDATE,'Month') = to_char(sysdate, 'Month')
         AND TRUNC(MONTHS_BETWEEN(SYSDATE, A.BIRTHDATE ))/12 = 21)


    Bob
    --
    Bob Fogarty
    LionPATH - Reporting Team Lead
    The Pennsylvania State University



    Alliance 2026 Registration is Open!


  • 4.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 07-15-2025 12:52 PM

    Bob,

    Great SQL advice.  I use a similar one for a process I have.  Using TRUNC with MONTHS_BETWEEN is what worked for me.



    ------------------------------
    Daniel Labrecque
    Senior 2 Business Systems Analyst/Functional Architect
    University of Nevada-Las Vegas
    ------------------------------

    Message from the HEUG Marketplace:
    ------------------------------
    Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 5.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 07-15-2025 01:40 PM

    If I'm not mistaken, this would return students who have already turned 21 in the month in question wouldn't it?  Since you're checking that the "birth month" is the current month and trunc(months_between... would leave any fractional parts off?



    ------------------------------
    Scott Nishizaki
    Developer/Analyst
    Azusa Pacific University
    ------------------------------

    Message from the HEUG Marketplace:
    ------------------------------
    Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 6.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 07-16-2025 06:47 AM
    The SQL I provided will return all turning 21 years old in the current month regardless of number of days in the month.

    Bob
    --
    Bob Fogarty
    LionPATH - Reporting Team Lead
    The Pennsylvania State University



    Alliance 2026 Registration is Open!


  • 7.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 07-15-2025 10:42 AM

    I'll take a look! Thanks!



    ------------------------------
    Richard Pope
    Senior Assistant Registrar for CU-SIS Development
    University of Colorado Colorado Springs
    ------------------------------

    Message from the HEUG Marketplace:
    ------------------------------
    Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 8.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 07-15-2025 01:47 PM

    I think you can get away with continuing with your current criteria set up as pictured above, but instead of using the +30 outside of add months, put and + 1 inside of it?  So the 2nd expression would become

    ADD_MONTHS(SYSDATE, -12*21 + 1)


    ------------------------------
    Scott Nishizaki
    Developer/Analyst
    Azusa Pacific University
    ------------------------------

    Message from the HEUG Marketplace:
    ------------------------------
    Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 9.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 07-16-2025 03:07 PM
    Edited by Scott Douglas 07-16-2025 03:30 PM

    As I test these options out, I am finding a small issue with Bob's SQL as well when run in our Oracle database. When I use MONTHS_BETWEEN() with today's date and the birthdate, the number returned is a decimal. Our target is 252 months, but if you run the SQL on 7/1/2025, only 7/1/2004 will come out to exactly 252 using MONTHS_BETWEEN(). 7/2/2004 will return ~ 251.97 months.  When you wrap that with TRUNC(), that turns into 251, which is less than 21 when divided by 12.

    A quick fix to this, at least in our database, is to use CEIL() instead of TRUNC(). This, when run on the first of the month, will take all the fractional MONTHS_BETWEEN values and round them up to the nearest integer. For instance, when run on 7/1/2025, you would get:

    Birthdate    CEIL(MONTHS_BETWEEN(SYSDATE, TO_CHAR(A.BIRTHDATE,'YYYY-MM-DD')))
    ----------   ---
    06/30/2004   251
    07/01/2004   252
    07/02/2004   252
    07/31/2004   252
    08/01/2004   253

    Here's that expression to get years instead of months:

    CEIL(MONTHS_BETWEEN(SYSDATE, TO_CHAR(A.BIRTHDATE,'YYYY-MM-DD') ))/12

    Also, it appears that Scott Nishizaki's suggestion would work, perhaps with less modification to the original user query:

    AND A.BIRTHDATE
      BETWEEN ADD_MONTHS(SYSDATE, -12*21)
          AND ADD_MONTHS(SYSDATE, -12*21 + 1)


    ------------------------------
    Scott Douglas
    Sr. Business Systems Analyst, Student Information Systems
    Azusa Pacific University
    ------------------------------

    Alliance 2026 Registration is Open!


  • 10.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 08-04-2025 06:02 PM
    Edited by Kendall Vance 08-07-2025 05:59 PM

    Once gotcha to consider when using  BETWEEN,  which is inclusive, I think (mine is).  So this translates to something like  "where birthday <= 08/01/2004 and birthday <= 09/01/2004", for example.  So, 9/1 will appear in both Aug and Sept version of the report.   

    WHERE A.BIRTHDATE
      BETWEEN ADD_MONTHS(SYSDATE, -12*21)
          AND ADD_MONTHS(SYSDATE, -12*21 + 1)     --BETWEEN is inclusive, so this will include birthdays that fall on the first of the following month.
    
    
    --instead:
    
    WHERE A.BIRTHDATE >= ADD_MONTHS(TRUNC(SYSDATE),-21*12)    --added TRUNC() to anchor to beginning of month.  not needed if always running on 1st of month
         AND A.BIRTHDATE < ADD_MONTHS(TRUNC(SYSDATE,'MM'), -12*21 + 1))   



    ------------------------------
    Kendall Vance
    IT Technical Associate for Financial Aid Reporting
    Northern Illinois University
    ------------------------------

    Message from the HEUG Marketplace:
    ------------------------------
    Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
    ------------------------------

    Alliance 2026 Registration is Open!


  • 11.  RE: How to account for months with more/less than 30 days using ADD_MONTHS function

    Posted 08-04-2025 04:43 PM

    A bit of a different approach, but would something like this work? Basically, is the birth-month this month, and the birth year 21 years ago?... This should work for the current month when ran on any day of the month, not just the 1st if my reasoning is correct . (I am limited to using Query Manager, so I rely heavily on Expressions, e.g.):

    CASE
    WHEN
    TO_NUMBER(TO_CHAR(TO_DATE(A.BIRTHDATE), 'MM')) = TO_NUMBER(TO_CHAR(SYSDATE, 'MM'))
    AND TO_NUMBER(TO_CHAR(TO_DATE(A.BIRTHDATE), 'YYYY')) = TO_NUMBER(TO_CHAR(SYSDATE, 'YYYY')) - 21
    THEN 'T'
    ELSE 'F'
    END

    Note: I am a bit of a PS noob, so feedback/criticism welcomed.



    ------------------------------
    Kendall Vance
    IT Technical Associate for Financial Aid Reporting
    Northern Illinois University
    ------------------------------

    Message from the HEUG Marketplace:
    ------------------------------
    Find, Review, and Engage with Higher Education-focused solution providers, products, and services using the HEUG Marketplace.
    ------------------------------

    Alliance 2026 Registration is Open!