全部产品
阿里云办公

DATEDIFF

更新时间:2018-10-09 18:43:11

语法

  1. INT DATEDIFF(VARCHAR enddate, VARCHAR startdate)
  2. INT DATEDIFF(TIMESTAMP enddate, VARCHAR startdate)
  3. INT DATEDIFF(VARCHAR enddate, TIMESTAMP startdate)
  4. INT DATEDIFF(TIMESTAMP enddate, TIMESTAMP startdate)

入参

  • startdate

    TIMESTAMP类型或VARCHAR类型日期。VARCHAR日期格式:yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss。

  • enddate

    TIMESTAMP类型或VARCHAR类型日期。VARCHAR日期格式:yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss。

功能描述

计算从enddate到startdate两个时间的天数差值,返回整数。若有参数为null或解析错误,返回null。

示例

  • 测试数据
datetime1(VARCHAR) datetime2(VARCHAR) nullstr(VARCHAR)
2017-10-15 00:00:00 2017-09-15 00:00:00 null
  • 测试案例
  1. SELECT DATEDIFF(datetime1, datetime2) as int1,
  2. DATEDIFF(TIMESTAMP '2017-10-15 23:00:00',datetime2) as int2,
  3. DATEDIFF(datetime2,TIMESTAMP '2017-10-15 23:00:00') as int3,
  4. DATEDIFF(datetime2,nullstr) as int4,
  5. DATEDIFF(nullstr,TIMESTAMP '2017-10-15 23:00:00') as int5,
  6. DATEDIFF(nullstr,datetime2) as int6,
  7. DATEDIFF(TIMESTAMP '2017-10-15 23:00:00',TIMESTAMP '2017-9-15 00:00:00')as int7
  8. FROM T1
  • 测试结果
int1(INT) int2(INT) int3(INT) int4(INT) int5(INT) int6(INT) int7(INT)
30 31 -31 null null null 31