Oracle中date型数据检索

来源:互联网 发布:什么是办公软件 编辑:程序博客网 时间:2024/06/06 12:39
 

用date型字段进行数据检索时需要注意:数据格式化!

此数据格式化不同于用SimpleDateFormat("yyyy-MM-dd")对象进行格式化,而是用Oracle的内置函数to_date() 或标识符date来格式化。

例如:

        有个字段是date数据,我想查询某某年度的数据。

错误写法:select  *  from table_Name  where col_Name > '2009-10-14';

                  select  *  from table_Name  where col_Name like '2006%';

                  这样就报:ora-01861 文字与格式字符串不匹配!

正确写法:1. 加to_date()函数

                      select  *  from table_Name  where col_Name > to_date('2009-10-14','yyyy-MM-dd');

                  2. 加date标志
                      select  *  from table_Name  where col_Name > date'2009-10-14';

                  3.  加to_char()函数(这样使用不了索引,影响性能,一般不建议这样使用)

                       select  *  from table_Name  where to_char(col_Name,'yyyy-MM-dd') like '2006%';

原创粉丝点击