简体   繁体   English

Oracle-转换日期时间格式

[英]Oracle - Convert datetime format

I have a temp table. 我有一个临时表。

It has last_update column in 2/10/2018 6:01:50 PM datetime format. 它具有日期时间格式为2/10/2018 6:01:50 PM last_update列。

How can I write THE BEST QUERY to display all information that's updated on 02-Oct-2018 day ? 如何编写“最佳查询”以显示2018年10月2 日当天更新的所有信息?

You can use trunc function 您可以使用trunc函数

select *
  from tab
 where trunc(last_update) = date'2018-10-02'

Use trunc function for getting the same day: 使用trunc函数获取同一天:

trunc(last_update) = trunc(to_date('02-Oct-2018', 'DD-MONTH-YYYY'))

The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. TRUNC(日期)函数返回日期,其中日期的时间部分被截断为格式模型fmt指定的单位。 The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. 即使您为date指定了不同的datetime数据类型,返回的值始终是DATE数据类型。 If you omit fmt, then date is truncated to the nearest day . 如果省略fmt,则日期将被截断为最近的日期

You can also use format DD-MON-YYYY 您也可以使用DD-MON-YYYY格式

It is preferable to avoid TRUNC especially if you have an index on the column last_update . 最好避免使用TRUNC特别是在列last_update上有索引的情况下。 A simple where condition should be better and may be better performant. 一个简单的where条件应该更好,可以得到更好的高性能。

WHERE last_update >= date '2018-10-02' AND
      last_update <  date '2018-10-02' + 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM