DB2中的时间函数

这里只列出一个在工作中遇到的问题。
简单列的说明:
1、work_date: Date 类型
2、time_signin: timestamp 类型
现在要按年或者按月统计信息,所以需要用到模糊查询。
测试发现,模糊查询针对的是字符串,不能对Date或者timestamp使用,怎么解决这个问题呢?
肯定是要把work_date或者time_signin转换成字符串!
之前对Oracle比较熟,自然想到用to_char,但是发现下面这种用法也不对:
to_char(work_date,"yyyy-MM")

查了下资料,发现在DB2中,to_char对timestamp类型的有效,对Date使用会报错。
那好,可以通过time_signin使用了:
select count(*) 
from attendence t 
where t.is_late = 0 and user_id = '123' and to_char(t.time_signin,'yyyy-MM') like '2012-07%';

虽然可以,但是心里还是不爽啊,毕竟当时设计表的时候,就是想用work_date 列来统计,就再查资料,解雇发现,char方法可以用,而且用法很简单:
select count(*) 
from attendence t 
where t.is_late = 0 and user_id = '123' and char(t.work_date) like '2012-07%';

这下解决了~~
预报几天要下雨,外面天黑黑的,凉爽啊~

猜你喜欢

转载自lieva.iteye.com/blog/1586585