1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 时间差函数---timestampdiff

时间差函数---timestampdiff

时间:2020-01-08 17:34:09

相关推荐

时间差函数---timestampdiff

时间差函数在SQL中很常见,然而不同的数据库时间函数有些差别,下面简单比对下。

时间差函数:timestampdiff

MySQL

语法: timestampdiff(unit,begin,end)

begin和end可以为DATE或DATETIME类型,并且可允许参数为混合类型。

DB2

语法:timestampdiff(unit,char(begin-end))

begin和end需要为时间戳格式。若非时间戳类型,用timestamp()函数转换。

timestampdiff(unit,char(timestamp(begin)-timestamp(end)))

unit参数是确定(end-begin)时间差的单位

例子:计算一个人的年龄:

MySql:

select timestampdiff(year,CURRENT_DATE,BIRTH_DT)

DB2:

select timestampdiff(256,char(CURRENT_DATE - BIRTH_DT))

HIVE:(hive没有timstampdiff函数,因而可以根据年份相减进行计算)

1.select if(datediff(current_date,concat(substr(CURRENT_DATE,0,4),substr(BIRTH_DT,5,7)))>=0,

(substr(CURRENT_DATE,0,4)-SUBSTR(BIRTH_DT,0,4)),

(substr(CURRENT_DATE,0,4)-SUBSTR(BIRTH_DT,0,4)-1))

2.select if(datediff(current_date,concat(substr(CURRENT_DATE,0,4),substr(BIRTH_DT,5,7)))>=0,

(year(CURRENT_DATE)-year(BIRTH_DT)),

(year(CURRENT_DATE)-year(BIRTH_DT)-1))

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。