A和D作爲答案的原因歸結爲nvl
函數中的數據類型不匹配。兩個參數必須是相同的類型。
with test_data as
(
select 1 as prog_id, 1 as prog_cost, to_date('10-12-2015', 'MM-dd-yyyy') as start_date, sysdate as end_date from dual
union all
select 2, null, to_date('10-12-2015', 'MM-dd-yyyy'), sysdate from dual
union all
select 3, 1, to_date('10-12-2015', 'MM-dd-yyyy'), null from dual
union all
select 4, null, to_date('10-12-2015', 'MM-dd-yyyy'), null from dual
)
select nvl(add_months(end_date, 1), sysdate)
from test_data;
上述執行並返回結果的所有記錄...
with test_data as
(
select 1 as prog_id, 1 as prog_cost, to_date('10-12-2015', 'MM-dd-yyyy') as start_date, sysdate as end_date from dual
union all
select 2, null, to_date('10-12-2015', 'MM-dd-yyyy'), sysdate from dual
union all
select 3, 1, to_date('10-12-2015', 'MM-dd-yyyy'), null from dual
union all
select 4, null, to_date('10-12-2015', 'MM-dd-yyyy'), null from dual
)
select to_date(nvl(SYSDATE - end_date, SYSDATE))
from test_data;
因爲nvl
的兩個參數是不相同的數據類型的上述引發錯誤ORA-01847: day of month must be between 1 and last day of month
...
with test_data as
(
select 1 as prog_id, 1 as prog_cost, to_date('10-12-2015', 'MM-dd-yyyy') as start_date, sysdate as end_date from dual
union all
select 2, null, to_date('10-12-2015', 'MM-dd-yyyy'), sysdate from dual
union all
select 3, 1, to_date('10-12-2015', 'MM-dd-yyyy'), null from dual
union all
select 4, null, to_date('10-12-2015', 'MM-dd-yyyy'), null from dual
)
select nvl(months_between(start_date, end_date), 'ongoing')
from test_data;
和上一個一樣,上面會拋出錯誤ORA-01722: invalid number
,因爲nvl
的兩個參數不是t他是相同的數據類型。 months_between
返回一個數字,'ongoing'
是一個varchar ...
最後一個作品,因爲它轉換的months_between
返回到char
...
with test_data as
(
select 1 as prog_id, 1 as prog_cost, to_date('10-12-2015', 'MM-dd-yyyy') as start_date, sysdate as end_date from dual
union all
select 2, null, to_date('10-12-2015', 'MM-dd-yyyy'), sysdate from dual
union all
select 3, 1, to_date('10-12-2015', 'MM-dd-yyyy'), null from dual
union all
select 4, null, to_date('10-12-2015', 'MM-dd-yyyy'), null from dual
)
select nvl(to_char(months_between(start_date, end_date)), 'ongoing')
from test_data;
@that_young_man:謝謝 – anishjp