Oracle:如何在时间戳中添加分钟?
我需要为 Oracle 日期列中的值添加 30 分钟。 我在 SELECT 语句中通过指定
to_char(date_and_time + (.000694 * 31)
来
执行此操作,大多数情况下工作正常。但当时间位于 AM/PM 边界时则不然。例如,在 12:30
[下午] 基础上添加 30 分钟会返回 1:00
,即上午。我期望的答案是 13:00
。这样做的正确方法是什么?
I need to add 30 minutes to values in a Oracle date column. I do this in my SELECT statement by specifying
to_char(date_and_time + (.000694 * 31)
which works fine most of the time. But not when the time is on the AM/PM border. For example, adding 30 minutes to 12:30
[which is PM] returns 1:00
which is AM. The answer I expect is 13:00
. What's the correct way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
除了能够向日期添加天数之外,假设您使用的是
Oracle 9i
或更高版本,您还可以使用间隔数据类型,这可能更容易阅读,In addition to being able to add a number of days to a date, you can use interval data types assuming you are on
Oracle 9i
or later, which can be somewhat easier to read,所有其他答案基本上都是正确的,但我认为没有人直接回答你原来的问题。
假设您的示例中的“date_and_time”是类型为 DATE 或 TIMESTAMP 的列,我认为您只需将其更改为:
听起来
您的默认日期格式使用“HH”代码表示小时,而不是“HH24” 。
另外,我认为你的常数项既令人困惑又不精确。 我猜你所做的就是计算 (.000694) 大约是一分钟的值,然后将其乘以你想要添加的分钟数(示例中为 31,尽管你在文本中说的是 30)。
我还会从一天开始,并将其划分为您在代码中想要的单位。 在本例中,(1/48) 为 30 分钟; 或者如果您想为了清楚起见将其分解,您可以编写 ( (1/24) * (1/2) )。
这将避免舍入错误(除了浮点固有的舍入错误,这些错误在这里应该毫无意义)并且更清晰,至少对我来说。
All of the other answers are basically right but I don't think anyone's directly answered your original question.
Assuming that "date_and_time" in your example is a column with type DATE or TIMESTAMP, I think you just need to change this:
to this:
It sounds like your default date format uses the "HH" code for the hour, not "HH24".
Also, I think your constant term is both confusing and imprecise. I guess what you did is calculate that (.000694) is about the value of a minute, and you are multiplying it by the number of minutes you want to add (31 in the example, although you said 30 in the text).
I would also start with a day and divide it into the units you want within your code. In this case, (1/48) would be 30 minutes; or if you wanted to break it up for clarity, you could write ( (1/24) * (1/2) ).
This would avoid rounding errors (except for those inherent in floating point which should be meaningless here) and is clearer, at least to me.
之一
interval
是YEARWhere
interval
is one of来自 http://www.orafaq.com/faq/how_does_one_add_a_day_hour_minute_second_to_a_date_value
SYSDATE 伪列显示当前系统日期和时间。 SYSDATE 加 1 会将日期提前 1 天。 使用分数在日期上添加小时、分钟或秒
from http://www.orafaq.com/faq/how_does_one_add_a_day_hour_minute_second_to_a_date_value
The SYSDATE pseudo-column shows the current system date and time. Adding 1 to SYSDATE will advance the date by 1 day. Use fractions to add hours, minutes or seconds to the date
我更喜欢使用
interval
文字,因为interval '30' 分钟
或interval '5' Second
比interval '5' Second
更容易阅读code>30 / (24 * 60) 或5 / (24 * 60 * 69)
例如
some_date + 间隔“2”小时
some_date + Interval '30' 分钟
some_date + Interval '5' Second
some_date + Interval '2' Day
您还可以将多个单位组合成一个表达式:
some_date + Interval '2 3:06' day to 分钟
将 2 天 3 小时 6 分钟添加到日期值
上面也是标准 SQL,也适用于其他几个 DBMS。
手册中的更多详细信息: https://docs.oracle.com/database /121/SQLRF/sql_elements003.htm#SQLRF00221
I prefer using an
interval
literal for this, becauseinterval '30' minute
orinterval '5' second
is a lot easier to read then30 / (24 * 60)
or5 / (24 * 60 * 69)
e.g.
some_date + interval '2' hour
some_date + interval '30' minute
some_date + interval '5' second
some_date + interval '2' day
You can also combine several units into one expression:
some_date + interval '2 3:06' day to minute
Adds 2 days, 3 hours and 6 minutes to the date value
The above is also standard SQL and also works in several other DBMS.
More details in the manual: https://docs.oracle.com/database/121/SQLRF/sql_elements003.htm#SQLRF00221
就像这样,
我很容易地向系统日期添加了 10 分钟,并且始终优先使用数据库服务器功能而不是自定义功能。
like that very easily
i added 10 minutes to system date and always in preference use the Db server functions not custom one .
如果字段的数据类型是日期或时间戳,如果您添加以天数给出的正确数字(或者在您的情况下是一天的正确分数),Oracle 应该始终给出正确的结果。 因此,如果您尝试在 30 分钟内提高该值,您应该使用:
根据您提供的信息,我相信这就是您尝试做的事情,并且我非常确定它有效。
If the data type of the field is date or timestamp, Oracle should always give the correct result if you add the correct number given in number of days (or a the correct fraction of a day in your case). So if you are trying to bump the value in 30 minutes, you should use :
Based on the information you provided, I believe this is what you tried to do and I am quite sure it works.
我们可以不使用这个吗?
我是这个领域的新手。
Can we not use this
I am new to this domain.
确保 Oracle 知道开始时间是 PM,并为最终输出指定 HH24 格式掩码。
注意:HH:MI 中的
'AM'
只是 AM/PM 子午线指示器的占位符。 也可能是'PM'
Be sure that Oracle understands that the starting time is PM, and to specify the HH24 format mask for the final output.
Note: the
'AM'
in the HH:MI is just the placeholder for the AM/PM meridian indicator. Could be also'PM'
Oracle 现在有新的内置函数来执行此操作:
Oracle now has new built in functions to do this:
根据您的要求,您需要 to_char 的 HH24:MI 格式。
Based on what you're asking for, you want the HH24:MI format for to_char.
要在oracle中编辑日期,您可以尝试
To edit Date in oracle you can try
只需您可以将其与各种日期格式一起使用......
simply you can use this with various date format....