invantive documentationpart of invantive cloud
Invantive Support ForumsLog In / Sign Up

MONTHS_BETWEEN

Syntax

%%{init: {
  'theme': 'base',
  'flowchart': { 'padding': '7', 'nodeSpacing': '20', 'rankSpacing': '20' },
  'themeVariables': {
    'fontSize': '11px',
    'fontFamily': 'Arial'
  }
}}%%
flowchart LR
    Start((START)) --> MONTHS_BETWEEN[MONTHS_BETWEEN]:::quoted
    MONTHS_BETWEEN --> End((END))

Purpose

The MONTHS_BETWEEN SQL functions gets the number of months between two dates. The fractional part is determined using a 30-day month length.

Parameters:

  • Date 1 (datetime): first date.
  • Date 2 (datetime): second date.

Returns: A decimal with the number of months and fractional part. Negative when date 2 is before date 1. Positive otherwise.

Examples

The following example retrieves the number of months between January 1, 2017 and January 16, 2017:

select months_between(to_date('20170101', 'YYYYMMDD'), to_date('20170116', 'YYYYMMDD'))

-------------------
0.5

The following example retrieves the number of months between January 16, 2017 and January 1, 2017; the second date is before the first date:

select months_between(to_date('20170116', 'YYYYMMDD'), to_date('20170101', 'YYYYMMDD'))

-------------------
-0.5