with grey (id,TO_DATE,FROM_DATE) as (values ('1234','2016-07-12','2014-07-28'))
SELECT
CEIL(MONTHS_BETWEEN(TO_DATE,FROM_DATE)) as Months
FROM GREY
WHERE ID='1234'
with grey (id,TO_DATE,FROM_DATE) as (values ('1234',date('2016-07-12'),date('2014-07-28')))
SELECT
timestampdiff(64, cast(timestamp(TO_DATE) - timestamp(FROM_DATE) as char(22))) as Months
FROM GREY
WHERE ID='1234';
with grey (id,TO_DATE,FROM_DATE) as (values ('1234',date('2016-07-12'),date('2014-07-28')))
SELECT
timestampdiff(64, cast(timestamp(last_day(TO_DATE)) - timestamp(last_day(FROM_DATE)) as char(22))) as Months
FROM GREY
WHERE ID='1234';
2条答案
按热度按时间dced5bon1#
您可以使用MONTHS_BETWEEN和CEIL的组合
toe950272#
另一个选项是TIMESTAMPDIFF()
但是请注意,
timestampdiff()
规则意味着上面的返回值是23,而不是您想要的24。因为开始日期是该月的28号,结束日期是12号。您可以通过添加LAST_DAY()来获得所需的结果