[英]How to get the exact year,month,date between two dates in one column oracle sql
[英]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.