簡體   English   中英

如何在sql查詢oracle中獲取兩個月之間的年份以及月份和日期

[英]how to get year between two months along with month and dates in sql query oracle

我需要提取沒有年份,兩個月之間的月份,例如我的輸出看起來像1year 9months 我不知道如何顯示這樣的輸出,下面是我現在使用的查詢。

select round((MONTHS_BETWEEN(lease_end_date,lease_start_date)/12),1) Duration_In_Years
from lease_header

這樣的事情怎么樣? 閱讀代碼中的注釋:

SQL> with test (date_1, date_2) as
  2    -- sample dates; to test the query, modify these values
  3    (select date '2021-10-20', date '2020-09-01' from dual),
  4  temp as
  5    -- split result of MONTHS_BETWEEN to number of years and number of months
  6    (select
  7       trunc(months_between(date_1, date_2) / 12) yrs,
  8       round((months_between(date_1, date_2) / 12 - trunc(months_between(date_1, date_2) / 12)) * 12, 1) mon
  9     from test
 10    )
 11  -- final result
 12  select yrs || ' year(s) ' || mon || ' month(s)' result
 13  from temp;

RESULT
----------------------
1 year(s) 1,6 month(s)

SQL>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM