在 Oracle 上比较日期/时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7026976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Comparing Date/time on Oracle
提问by Kyle
Currently on my oracle table, I have the following if condition on my procedure
目前在我的 oracle 表上,我的程序有以下条件
if TO_CHAR(updatetime, 'DD-MON-YYYY HH:MI:SS') != '01-JAN-1900 00:00:01' then
Suppose the value updatetime from oracle table is "01-JAN-1900 12:00:01 AM", will my if condition be true or false ?
假设 oracle 表中的值 updatetime 是"01-JAN-1900 12:00:01 AM",我的 if 条件是真还是假?
The reason I'm asking is because I am unsure about the format TO_CHAR converts to. Value on the table is in a 12-Hr clock format, while the condition compares with a 24-hr format.
我问的原因是因为我不确定 TO_CHAR 转换为的格式。表中的值采用 12 小时时钟格式,而条件则与 24 小时格式进行比较。
This is my first week working with oracle sql..
这是我使用 oracle sql 的第一周。
Thanks !
谢谢 !
回答by StevieG
What @Tony Andrews said, but you probably want to make your to_char convert to 24 hour clock if you're comparing it to a string containing 24 hour clock value. Change HH to HH24 like this:
@Tony Andrews 所说的,但如果您将 to_char 与包含 24 小时制值的字符串进行比较,您可能希望将其转换为 24 小时制。像这样将 HH 更改为 HH24:
if TO_CHAR(updatetime, 'DD-MON-YYYY HH24:MI:SS') != '01-JAN-1900 00:00:01' then
回答by Tony Andrews
Oracle DATE columns do not hold the date as a formatted string, it is held (internally) as 7 numeric values: century, year, month, day, hour, minute, second. So it doesn't matter what format you use in your TO_CHAR.
Oracle DATE 列不将日期保存为格式化字符串,它(内部)保存为 7 个数值:世纪、年、月、日、小时、分钟、秒。因此,您在 TO_CHAR 中使用什么格式并不重要。