当前位置:数据库 > DB2 >>

DB2开发常用-----日期函数(常用日期函数)

DB2开发常用-----日期函数(常用日期函数)
 
获取时间
 
1、获取当期日期:
 
values current date;
 
2012-08-28
 
2、获取当期时间
 
values current time;
 
11:56:36
 
3、获取当前时间戳
 
values current timestamp;
 
2012-08-28 11:57:32
 
4、year()
 
获取当前年份
 
values year(current timestamp);
 
2012
 
5、month()
 
   获取当前月份
 
values month(current timestamp);
 
8
 
6、day()
 
    获取当前日
 
values day(current timestamp);
 
28
 
7、 hour()
 
    获取当前时
 
values hour(current timestamp);
 
12
 
8、minute()
 
    获取当前分
 
values minute(current timestamp);
 
3
 
9、second()
 
    获取秒
 
values second(current timestamp);
 
48
 
10、microsecond()
 
    获取毫秒
 
values microsecond(current timestamp);
 
59000
 
11、timestamp转varchar
 
values varchar_format(current timestamp,'yyyy-mm-dd hh24-mm-ss');
 
2012-08-28 12-08-21
 
12、timestamp中抽取date
 
values date(current timestamp);
 
2012-08-28
 
13、timestamp中抽取time
 
values time(current timestamp);
 
12:14:51
 
14、星期相关
 
Dayname()返回日期参数中的星期几,返回值类型:字符串;例如:星期一
 
values dayname(current timestamp);
 
Tuesday
 
Dayofweek()返回日期参数中的星期几,返回值类型:整数;例如:1;其中1代表星期日
 
values dayofweek(current timestamp);
 
----今天是Tuesday
 
Dayofweek_iso()返回日期参数中的星期几,返回值类型:整数;例如:1;其中1代表星期一
 
values dayofweek_iso(current timestamp);
 
2
----今天是Tuesday
 
Week()返回日期参数中所在年的第几周,返回范围在(1-54)的整数,以星期日作为一周的开始
 
values week(timestamp('2012-1-8'));
 
2
 
Week()返回日期参数中所在年的第几周,返回范围在(1-53)的整数,以星期一作为一周的开始
 
values week_iso(timestamp('2012-1-8'));
 
1
 
15、时间与字符串之间转换
 
Varchar_format(<time>,’yyyy-mm-dd’)返回值:字符串
 
 
values varchar_format(current timestamp,'yyyy-mm-dd hh24-mm-ss');
 
2012-08-28 12-08-37
 
To_char()
 
 
values to_char(current timestamp);
 
Aug 28, 2012 12:37:33 PM
 
Char()
 
 
values char(current timestamp);
 
2012-08-28-12.38.10.387000
 
values char(time('22:24:23'));
 
22.24.23
 
字符串转日期或时间
 
 
Date()
 
 
values date('2012-1-1');
 
2012-01-01
 
Time()
 
 
values time('22.22.22');
 
22:22:22
 
Timestamp()
 
 
values timestamp('2012-1-1-22.42.23.000890');
 
2012-01-01 22:42:23
 
16、时间计算
 
 
values current date+1 year+2 months+4 days;
 
2013-11-01
-----2012-08-28
 
17、时间差计算
 
Timestampdiff()
 
前提条件:1、不考虑闰年;2、假设每个月只有30天
 
1 = 秒的小数部分 
2 = 秒 
4 = 分 
8 = 时 
16 = 天 
32 = 周 
64 = 月 
128 = 季度 
256 = 年
 
timestampdiff(2,char(current timestamp - timestamp(task.create_)))
 
精确计算()返回值:整数
 
(DAYS(<timestamp1>) - DAYS(<timestamp2>)) * 86400 + 
(MIDNIGHT_SECONDS(<timestamp1>) - MIDNIGHT_SECONDS(<timestamp2>))
`
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,