我帮楼主问一下。。 为什么用 where to_char(mydate , 'yyyymmdd') = '20040409' 能够找到数据?
表中的字段mydate一定带有时间了
同意楼上各位的 你可以 SQL>SELECT mydate FROM MY_TABLE 看一下
select * from my_table where tochar(mydate,'yyyy-mm-dd') = '2004-04-09'; 或 select * from my_table where to_date(tochar(mydate,'yyyy-mm-dd'),'yyyy-mm-dd') = to_date ('2004-04-09','yyyy-mm-dd');
看到to_char函数了吗?这是一个转换函数,要转换成一直的类型才好比较的啊.你具体查一下to_char函数看看具体说明呢. 一般按时间比较的都是select * from tablename a where to_char(a.userdate,'格式字符串') >=to_cahr(.....)
明白大家的意思了,确实是含有时间看来我应该这样用 select * from my_table where mydate between to_date ('2004-04-09 00:00:00','yyyy-mm-dd h24:mi:ss') and ('2004-04-09 23:59:59','yyyy-mm-dd h24:mi:ss') 来检索4.9当天的数据,或者 where mydate between to_date ('2004-04-09') and to_date ('2004-04-10') --按drabit(square)的意思to Supernpc(一生之火): where to_char(mydate , 'yyyymmdd') = '20040409' 能够找到数据因为mydate被转换为字符串了,它可以和'20040409'这样的字符串进行比较。以不才愚见 select * from my_table where mydate between to_date ('2004-04-09 00:00:00','yyyy-mm-dd h24:mi:ss') and ('2004-04-09 23:59:59','yyyy-mm-dd h24:mi:ss') 应该是最好的解决方案,因为它只作2次数据类型转换,后面的都是比较 而where to_char(mydate , 'yyyymmdd') betweent '20040409' and …… 这样的语句在同等条件下要对表中每一条mydate数据都作类型转换和比较,效率比较差不知各位有何意见:)
在实际应用中,有的时候要用between and 但是根据实际情况,我很少用的.我一般是拆开: select * from tablename tbl where to_char(tbl.date,'yyyymmdd')>=某个日期 and to_char(tbl.date,'yyyymmdd')<=某个日期
to dinya2003() : between and 和>= <=有什么不同呢?性能上有分别么?
up:) skystar99047(天星) between and 和>= <= 在业务逻辑上是一样的。至于性能上就不太清楚了。
你可以这样写: select * from my_table where trunc(mydate) = to_date('2004-04-09','yyyy-mm-dd')
只含年月日,2004-04-09 12:21:21当然不等于2004-04-09了,原因就是这样
为什么用
where to_char(mydate , 'yyyymmdd') = '20040409'
能够找到数据?
你可以
SQL>SELECT mydate FROM MY_TABLE
看一下
where tochar(mydate,'yyyy-mm-dd') = '2004-04-09';
或
select * from my_table
where to_date(tochar(mydate,'yyyy-mm-dd'),'yyyy-mm-dd') = to_date ('2004-04-09','yyyy-mm-dd');
一般按时间比较的都是select * from tablename a where to_char(a.userdate,'格式字符串') >=to_cahr(.....)
select * from my_table
where mydate between to_date ('2004-04-09 00:00:00','yyyy-mm-dd h24:mi:ss') and ('2004-04-09 23:59:59','yyyy-mm-dd h24:mi:ss')
来检索4.9当天的数据,或者
where mydate between to_date ('2004-04-09') and to_date ('2004-04-10') --按drabit(square)的意思to Supernpc(一生之火):
where to_char(mydate , 'yyyymmdd') = '20040409'
能够找到数据因为mydate被转换为字符串了,它可以和'20040409'这样的字符串进行比较。以不才愚见
select * from my_table
where mydate between to_date ('2004-04-09 00:00:00','yyyy-mm-dd h24:mi:ss') and ('2004-04-09 23:59:59','yyyy-mm-dd h24:mi:ss')
应该是最好的解决方案,因为它只作2次数据类型转换,后面的都是比较
而where to_char(mydate , 'yyyymmdd') betweent '20040409' and ……
这样的语句在同等条件下要对表中每一条mydate数据都作类型转换和比较,效率比较差不知各位有何意见:)
select * from tablename tbl where to_char(tbl.date,'yyyymmdd')>=某个日期
and to_char(tbl.date,'yyyymmdd')<=某个日期
between and 和>= <=有什么不同呢?性能上有分别么?
select * from my_table
where trunc(mydate) = to_date('2004-04-09','yyyy-mm-dd')