时间参数在oracle sql查询中不起作用

时间参数在oracle sql查询中不起作用,sql,oracle,Sql,Oracle,我通过这个sql查询我想计算下午5:00:00之后和之前的记录 05:00:00 PM。这里是我的查询,但是通过这个我得到了错误的结果。请任何人检查这个,如果这个中有任何逻辑错误 Select trunc(gross_weight_date) date1, count(*) before5 from wbg.WBG_01_01 where to_char(gross_weight_date,'HH:MI:SS PM')>'05:00:00 PM' and item_cod = 16 an

我通过这个sql查询我想计算下午5:00:00之后和之前的记录 05:00:00 PM。这里是我的查询,但是通过这个我得到了错误的结果。请任何人检查这个,如果这个中有任何逻辑错误

Select trunc(gross_weight_date) date1,
count(*) before5 
from wbg.WBG_01_01
where to_char(gross_weight_date,'HH:MI:SS PM')>'05:00:00 PM'
and item_cod = 16
and trunc(gross_weight_date)='05-JAN-2012' 
group by trunc(gross_weight_date)
order by date1
您正在使用chars比较日期,可能会得到任何帮助。那不能正常工作

由于您只想在下午5点之后进行比较,因此这里有一个更简单的解决方案:

Select trunc(gross_weight_date) date1,
count(*) before5 
from wbg.WBG_01_01
where to_number(to_char(gross_weight_date, 'HH24MISS')) > 150000
and item_cod = 16
and trunc(gross_weight_date)='05-JAN-2012' 
group by trunc(gross_weight_date)
order by date1
它将日期值传递给字符“171212”,然后传递给数字171212,并将其与150000进行比较。

您正在使用字符来比较日期。那不能正常工作

由于您只想在下午5点之后进行比较,因此这里有一个更简单的解决方案:

Select trunc(gross_weight_date) date1,
count(*) before5 
from wbg.WBG_01_01
where to_number(to_char(gross_weight_date, 'HH24MISS')) > 150000
and item_cod = 16
and trunc(gross_weight_date)='05-JAN-2012' 
group by trunc(gross_weight_date)
order by date1
它将日期值传递给字符“171212”,然后传递给数字171212,并将其与150000进行比较

不要使用VARCHAR2比较日期,因为字符的排序顺序与数字“12”在“5”之前的顺序不同。 不要把苹果和桔子做比较。树干毛重日期是一个日期,而“2012年1月5日”是一个日期。 处理日期时,可以使用日期函数和日期算术,而无需进行转换,例如:

Select trunc(gross_weight_date) date1, count(*) before5 
  from wbg.WBG_01_01
 where item_cod = 16
   and gross_weight_date > to_date('05-01-2012', 'DD-MM-YYYY') + 17/24 
   and gross_weight_date < to_date('05-01-2012', 'DD-MM-YYYY') + 1 
 group by trunc(gross_weight_date)
 order by date1

不要使用VARCHAR2比较日期,因为字符的排序顺序与数字“12”在“5”之前的顺序不同。 不要把苹果和桔子做比较。树干毛重日期是一个日期,而“2012年1月5日”是一个日期。 处理日期时,可以使用日期函数和日期算术,而无需进行转换,例如:

Select trunc(gross_weight_date) date1, count(*) before5 
  from wbg.WBG_01_01
 where item_cod = 16
   and gross_weight_date > to_date('05-01-2012', 'DD-MM-YYYY') + 17/24 
   and gross_weight_date < to_date('05-01-2012', 'DD-MM-YYYY') + 1 
 group by trunc(gross_weight_date)
 order by date1


请问如何兑换elloborate@user1397781您现在可以试一试:请问如何转换elloborate@user1397781您现在可以尝试:可能重复的可能重复的可能重复的只是想知道是否有任何特殊的原因来添加间隔,而不是在掩码中使用时间:截止日期'05-01-2012 17:00',“DD-MM-YYYY HH24:MI”?@glenn:日期组件和时间组件很可能是变量/参数。在这种情况下,添加它们而不使用to_CHAR转换是有意义的。对于常数,我同意使用时间掩码更容易阅读。我明白了,这很有意义。谢谢。我只是想知道是否有什么特别的原因来添加时间间隔而不是在掩码中使用时间:to_date'05-01-2012 17:00','DD-MM-yyyyy HH24:MI'?@glenn:日期组件和时间组件很可能是变量/参数。在这种情况下,添加它们而不使用to_CHAR转换是有意义的。对于常数,我同意使用时间掩码更容易阅读。我明白了,这很有意义。谢谢