简体   繁体   English

在Oracle中比较日期时间

[英]Comparing Datetime in Oracle

Do I need to convert TIMESTAMP to DATE before doing a comparison? 做比较之前,我需要将TIMESTAMP转换为DATE吗? EX: 例如:

AND SORD.CREATED >= TO_DATE(FROM_TZ(CAST(TO_DATE('','DD-MON-YYYY HH24:MI:SS') AS TIMESTAMP), 'Asia/Kuala_Lumpur') AT TIME ZONE 'UTC','DD-MON-YYYY HH24:MI:SS')
AND SORD.CREATED < TO_DATE(FROM_TZ(CAST(TO_DATE('','DD-MON-YYYY HH24:MI:SS') AS TIMESTAMP), 'Asia/Kuala_Lumpur') AT TIME ZONE 'UTC','DD-MON-YYYY HH24:MI:SS')

Just looking for an opinion, since I'm still new and learning about databases. 只是想征求意见,因为我还是新手,正在学习数据库。

It's hard to tell exactly what you database you are using. 很难准确说明正在使用的数据库。 From the functions in the code you provided, I am guessing Oracle? 从您提供的代码中的功能来看,我猜是Oracle吗? Please confirm. 请确认。

In general, I can tell you the following: 通常,我可以告诉您以下内容:

  • You aren't passing any values. 您没有传递任何值。 The first parameter to TO_DATE is empty, and that would contain the string to convert to a date. TO_DATE的第一个参数为空,其中将包含要转换为日期的字符串。

  • You need to know what the values in your database are. 您需要知道数据库中的值是什么。 Is SORD.CREATED a DATE or TIMESTAMP datatype? SORD.CREATEDDATE还是TIMESTAMP数据类型? Does it keep UTC values? 是否保留UTC值? If so, then yes, you should convert local times to UTC before querying. 如果是这样,那么是的,您应该在查询之前将本地时间转换为UTC。 If possible, you should do this outside of the query itself. 如果可能,您应该在查询本身之外执行此操作。

  • If the values in your database are of type TIMESTAMP WITH TIME ZONE , then you can use a DATE or TIMESTAMP with the AT TIME ZONE function, like you showed in your question. 如果数据库中的值的类型为TIMESTAMP WITH TIME ZONE ,则可以将DATETIMESTAMPAT TIME ZONE函数一起使用,就像您在问题中所显示的那样。

  • Don't cast to/from a string unnecessarily. 不要不必要地将字符串强制转换为字符串。 If your source data is already in a DATE or TIMESTAMP datatype, there's no need to convert to a string. 如果您的源数据已经是DATETIMESTAMP数据类型,则无需转换为字符串。 Once a date is a date, you should keep it a date. 日期一旦成为日期,就应保留其日期。 Introducing string formatting can sometimes introduce errors. 引入字符串格式有时会引入错误。

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

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