---
title: "MONTHS_BETWEEN"
description: "MONTHS_BETWEEN SQL\u95A2\u6570\u306F\u30012\u3064\u306E\u65E5\u4ED8\u306E\u9593\u306E\u6708\u6570\u3092\u53D6\u5F97\u3057\u307E\u3059\u3002"
language: ja
source_language: en
translation_state: complete
canonical: https://documentation.invantive.eu/ja/docs/invantive-universalsql/grammar/sql-functions/months-between
original: https://documentation.invantive.eu/en/docs/invantive-universalsql/grammar/sql-functions/months-between
updated: 2026-12-04T14:01:46Z
license: https://documentation.invantive.eu/ja/
---

## 構文
```mermaid
%%{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))
```

## 目的
`MONTHS_BETWEEN` SQL関数は、2つの日付の間の月数を取得します。 小数部分は、30日の月の長さを使用して決定されます。

パラメータ:
- 日付 1 (`datetime`): 1番目の日付。
- 日付 2 (`datetime`): 2番目の日付。

戻り値: 月数と小数部分を含む `decimal` 型の値。 日付 2 が日付 1 より前の場合、負の値になります。 それ以外の場合は正の値になります。

## 例
次の例では、1月 1、2017 と 1月 16、2017 の間の月数を取得します:
```sql
select months_between(to_date('20170101', 'YYYYMMDD'), to_date('20170116', 'YYYYMMDD'))

-------------------
0.5
```
次の例では、1月 16、2017 と 1月 1、2017 の間の月数を取得します。2番目の日付は1番目の日付より前です:
```sql
select months_between(to_date('20170116', 'YYYYMMDD'), to_date('20170101', 'YYYYMMDD'))

-------------------
-0.5
```

