您的位置:首页 > 其它

DB2日期—时间—时间戳的运算

2013-03-27 06:38 155 查看
1、日期值的运算。

在日期上增加或者减去年,月,日时,可直接在增加或者减少的值后面直接跟着年月日的英文单词,

即year,month,day;大于1则使用复数形式,如下:

values (current date,             --2013-03-26(current_date)

        current date + 10 years,  --2023-03-26

        current date - 13 years,  --2000-03-26

        current date + 13 months, --2014-04-26

        current date - 24 months, --2011-03-26

        current date + 60 days,   --2013-05-25

        current date - 26 days    --2013-02-28

       )

2、时间运算

在指定时间值上面增加或者减去时分秒值,在增减数据值后面跟着:hour,minute,second。

values (current_time,              --18:59:54

        current_time + 10 hours,   --04:59:54

        current_time - 23 hours,   --19:59:54

        current_time + 12 minutes, --19:11:54

        current_time - 63 minutes, --17:56:54

        current_time + 25 seconds, --19:00:19

        current_time - 61 seconds  --18:58:53

       )

需要说明的是,当增加或者减少的数值为浮点数的时候,只会计算整数部分,

小数部分不会影响之后的运算,先后顺序:年 -> 月 -> 日 -> 时 -> 分 -> 秒

3、时间戳的运算

综合了第1点和第2点的运算。

values (current_timestamp,                  --2013-03-26-19.47.52.414000

        current_timestamp + 10.5 years,     --2023-03-26-19.47.52.414000

        current_timestamp - 4 months,       --2012-11-26-19.47.52.414000

        current_timestamp + 26 days,        --2013-04-21-19.47.52.414000

        current_timestamp + 7 hours,        --2013-03-27-02.47.52.414000

        current_timestamp - 5.2 minutes,    --2013-03-26-19.42.52.414000

        current_timestamp - 1 second,       --2013-03-26-19.47.51.414000

        current_timestamp + 10 microseconds --2013-03-26-19.47.52.414010
       )

4、若不考虑闰年,每月按照30天计算的话,可以使用timestampdiff()函数,详情请参照:

http://blog.csdn.net/bobo12082119/article/details/8724583

http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000861.html

转载请注明出处:http://blog.csdn.net/bobo12082119/article/details/8724586

--the end--
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: