前一阵子需要修复一些数据要出脚本,对数据库的查找和修改操作比较多,表中有一个字段是timestamp类型,而且要用这个字段作为条件查找数据,怎么方便的操作这个字段呢?
(注:
t_table 表
begin_time 字段:timestamp类型)
select t.* from t_table t where cast(t.begin_time as DATE) >= to_date('2015-01-10 08:00:01','yyyy-mm-dd hh24:mi:ss') ;
其实就是将timestamp用cast转换成date类型,再和to_date('时间字符串','时间格式')比较,这样我就可以直接写一个时间字符串(例如:2015-01-10 08:00:01)就ok了。
再来说说插入语句:(假设就增加这一个字段,主键什么的先忽略)
insert into t_table(begin_time) values (CAST(to_date('2015-06-06 10:38:22','yyyy-mm-dd hh24:mi:ss')as timestamp));
其实就是将字符串转换成date类型,再将这个date类型数据用CAST转换成表中需要的timestamp类型数据。
总结:CAST 就是进行类型转换的,CAST
converts values from one data type to another.(官方文档这样解释)。
这里只是针对时间类型进行了一些转换,cast还可以对其他数据类型转换。