将日期和时间组合到日期时间

时间:2018-02-19 10:58:25

标签: sql oracle

我想合并日期&时间。我用过'CONCAT'功能与也' ||'功能。但是这两个函数花费更多次并且查询速度非常慢。有人能告诉我如何缩短查询时间吗? &安培;什么功能可用于合并日期和&时间?谢谢你的帮助。

    where CONCAT (TO_CHAR (Date, 'YYYYMMDD'), LPAD (Time, 6, 0))
between CONCAT ('20180215', '130528') AND CONCAT ('20180215', '133003')

2 个答案:

答案 0 :(得分:2)

您无需将日期转换为字符串 - 只需将静态值转换为日期:

WHERE "Date" BETWEEN TO_DATE( '20180215130528', 'YYYYMMDDHH24MISS' )
                 AND TO_DATE( '20180215133003', 'YYYYMMDDHH24MISS' )

如果您想使用CONCAT,那么:

WHERE "Date" BETWEEN TO_DATE( CONCAT( '20180215', '130528' ), 'YYYYMMDDHH24MISS' )
                 AND TO_DATE( CONCAT( '20180215', '133003' ), 'YYYYMMDDHH24MISS' )

您还可以使用TIMESTAMP字面值:

WHERE "Date" BETWEEN TIMESTAMP '2018-02-15 13:05:28'
                 AND TIMESTAMP '2018-02-15 13:30:03'

答案 1 :(得分:0)

您希望在日期上设置索引以加快查询速度 更好的方法是将日期和时间合并到一个字段Date中,类型为Date(并且在Date上有索引)。 这将为您提供简单的高性能查询:

WHERE Date between to_date('20180215' || '130528', 'YYYYMMDDHH24MISS') and to_date('20180215'||'133003', 'YYYYMMDDHH24MISS') 

对于您当前的方案,您将获得更好的性能结构,如下所示:

where Date between to_date('20180215', 'YYYYMMDD') and to_date('20180215', 'YYYYMMDD') and Date || LPAD (Time, 6, 0)) between CONCAT ('20180215', '130528') AND CONCAT ('20180215', '133003')