Oracle:如何在时间戳中添加分钟?

发布于 2024-07-07 22:25:37 字数 267 浏览 13 评论 0原文

我需要为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

听,心雨的声音 2024-07-14 22:25:37

除了能够向日期添加天数之外,假设您使用的是 Oracle 9i 或更高版本,您还可以使用间隔数据类型,这可能更容易阅读,

SQL> ed
Wrote file afiedt.buf
SELECT sysdate, sysdate + interval '30' minute FROM dual
SQL> /

SYSDATE              SYSDATE+INTERVAL'30'
-------------------- --------------------
02-NOV-2008 16:21:40 02-NOV-2008 16:51:40

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,

SQL> ed
Wrote file afiedt.buf
SELECT sysdate, sysdate + interval '30' minute FROM dual
SQL> /

SYSDATE              SYSDATE+INTERVAL'30'
-------------------- --------------------
02-NOV-2008 16:21:40 02-NOV-2008 16:51:40
凉宸 2024-07-14 22:25:37

所有其他答案基本上都是正确的,但我认为没有人直接回答你原来的问题。

假设您的示例中的“date_and_time”是类型为 DATE 或 TIMESTAMP 的列,我认为您只需将其更改为:

to_char(date_and_time + (.000694 * 31))

听起来

to_char(date_and_time + (.000694 * 31), 'DD-MON-YYYY HH24:MI')

您的默认日期格式使用“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_char(date_and_time + (.000694 * 31))

to this:

to_char(date_and_time + (.000694 * 31), 'DD-MON-YYYY HH24:MI')

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.

无人接听 2024-07-14 22:25:37
UPDATE "TABLE" 
SET DATE_FIELD = CURRENT_TIMESTAMP + interval '48' minute 
WHERE (...)

之一

  • 其中 interval 是YEAR
  • MONTH
  • DAY
  • HOUR
  • MINUTE
  • SECOND
UPDATE "TABLE" 
SET DATE_FIELD = CURRENT_TIMESTAMP + interval '48' minute 
WHERE (...)

Where interval is one of

  • YEAR
  • MONTH
  • DAY
  • HOUR
  • MINUTE
  • SECOND
国际总奸 2024-07-14 22:25:37

来自 http://www.orafaq.com/faq/how_does_one_add_a_day_hour_minute_second_to_a_date_value

SYSDATE 伪列显示当前系统日期和时间。 SYSDATE 加 1 会将日期提前 1 天。 使用分数在日期上添加小时、分钟或秒

SQL> select sysdate, sysdate+1/24, sysdate +1/1440, sysdate + 1/86400 from dual;

SYSDATE              SYSDATE+1/24         SYSDATE+1/1440       SYSDATE+1/86400
-------------------- -------------------- -------------------- --------------------
03-Jul-2002 08:32:12 03-Jul-2002 09:32:12 03-Jul-2002 08:33:12 03-Jul-2002 08:32:13

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

SQL> select sysdate, sysdate+1/24, sysdate +1/1440, sysdate + 1/86400 from dual;

SYSDATE              SYSDATE+1/24         SYSDATE+1/1440       SYSDATE+1/86400
-------------------- -------------------- -------------------- --------------------
03-Jul-2002 08:32:12 03-Jul-2002 09:32:12 03-Jul-2002 08:33:12 03-Jul-2002 08:32:13
烟酒忠诚 2024-07-14 22:25:37

我更喜欢使用 interval 文字,因为 interval '30' 分钟interval '5' Secondinterval '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, because interval '30' minute or interval '5' second is a lot easier to read then 30 / (24 * 60) or 5 / (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

青衫儰鉨ミ守葔 2024-07-14 22:25:37

就像这样,

我很容易地向系统日期添加了 10 分钟,并且始终优先使用数据库服务器功能而不是自定义功能。

select to_char(sysdate + NUMTODSINTERVAL(10,'MINUTE'),'DD/MM/YYYY HH24:MI:SS') from dual;

like that very easily

i added 10 minutes to system date and always in preference use the Db server functions not custom one .

select to_char(sysdate + NUMTODSINTERVAL(10,'MINUTE'),'DD/MM/YYYY HH24:MI:SS') from dual;
九局 2024-07-14 22:25:37

如果字段的数据类型是日期或时间戳,如果您添加以天数给出的正确数字(或者在您的情况下是一天的正确分数),Oracle 应该始终给出正确的结果。 因此,如果您尝试在 30 分钟内提高该值,您应该使用:

select field + 0.5/24 from table;

根据您提供的信息,我相信这就是您尝试做的事情,并且我非常确定它有效。

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 :

select field + 0.5/24 from table;

Based on the information you provided, I believe this is what you tried to do and I am quite sure it works.

(り薆情海 2024-07-14 22:25:37

我们可以不使用这个吗?

SELECT date_and_time + INTERVAL '20:00' MINUTE TO SECOND FROM dual;

我是这个领域的新手。

Can we not use this

SELECT date_and_time + INTERVAL '20:00' MINUTE TO SECOND FROM dual;

I am new to this domain.

怪异←思 2024-07-14 22:25:37

确保 Oracle 知道开始时间是 PM,并为最终输出指定 HH24 格式掩码。

SELECT to_char((to_date('12:40 PM', 'HH:MI AM') + (1/24/60) * 30), 'HH24:MI') as time
  FROM dual

TIME
---------
13:10

注意: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.

SELECT to_char((to_date('12:40 PM', 'HH:MI AM') + (1/24/60) * 30), 'HH24:MI') as time
  FROM dual

TIME
---------
13:10

Note: the 'AM' in the HH:MI is just the placeholder for the AM/PM meridian indicator. Could be also 'PM'

往事风中埋 2024-07-14 22:25:37

Oracle 现在有新的内置函数来执行此操作:

select systimestamp START_TIME, systimestamp + NUMTODSINTERVAL(30, 'minute') end_time from dual

Oracle now has new built in functions to do this:

select systimestamp START_TIME, systimestamp + NUMTODSINTERVAL(30, 'minute') end_time from dual
放低过去 2024-07-14 22:25:37

根据您的要求,您需要 to_char 的 HH24:MI 格式。

Based on what you're asking for, you want the HH24:MI format for to_char.

彩虹直至黑白 2024-07-14 22:25:37

要在oracle中编辑日期,您可以尝试

  select to_char(<columnName> + 5 / 24 + 30 / (24 * 60),
           'DD/MM/RRRR hh:mi AM') AS <logicalName> from <tableName>

To edit Date in oracle you can try

  select to_char(<columnName> + 5 / 24 + 30 / (24 * 60),
           'DD/MM/RRRR hh:mi AM') AS <logicalName> from <tableName>
小嗷兮 2024-07-14 22:25:37
SELECT to_char(sysdate + (1/24/60) * 30, 'dd/mm/yy HH24:MI am') from dual;

只需您可以将其与各种日期格式一起使用......

SELECT to_char(sysdate + (1/24/60) * 30, 'dd/mm/yy HH24:MI am') from dual;

simply you can use this with various date format....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文