数字转时间预测器

huangapple go评论60阅读模式
英文:

Number to Time Oracle

问题

我想知道是否可以得到一些帮助。我尝试将以下的42499转换成Oracle中的时间字段。该字段目前是一个varchar2。

谢谢。

我尝试使用以下方法转换:to_number(to_date(transactiontime, 'hh24ss'))

ORA-01850: 小时必须在0和23之间
01850. 00000 - "小时必须在0和23之间"
*原因:
*操作:

英文:

Just wondering if I might be able to get some help. I'm trying to convert the following 42499 into a time field in Oracle. The field is currently a varchar2

Thank you

I've tried to_number(to_date(transactiontime, 'hh24ss'))

ORA-01850: hour must be between 0 and 23
01850. 00000 - "hour must be between 0 and 23"
*Cause:
*Action:

答案1

得分: 1

Oracle没有一个仅包含时间的数据类型,但你可以将其转换为日期,就像你正在尝试的那样;如果你的值表示从午夜开始的秒数,则可以使用 sssss 格式模型

to_date(to_number(transactiontime), 'sssss')

你的客户端可能配置为仅显示日期或完整的日期时间,所以你可能会看到 01-MAY-232023-05-01 11:48:19 或其他内容。(如果你不指定任何日期元素,它会默认为当前月的第一天。)你可以使用 to_char() 将日期转换回字符串:

to_char(to_date(to_number(transactiontime), 'sssss'), 'HH24:MI:SS')

这将给出 11:48:19

你还可以将你的数字转换为间隔数据类型:

numtodsinterval(to_number(transactiontime), 'SECOND') 

或者

to_number(transactiontime) * interval '1' second

这两者都会给出 +000000000 11:48:19.000000000

你不能直接格式化它,但你可以使用以下方法截取你想要的部分:

substr(to_number(transactiontime) * interval '1' second, 12, 8)

fiddle

或者你可以将该间隔添加到一个固定的日期(午夜),比如日期文字字面量 date '1970-01-01';或者 - 如果出于某种原因你将交易日期和时间存储为两个单独的字段(这是极少情况下的合理做法) - 你可以将它添加到那个日期。然后,你可以按照你想要的方式格式化组合值。

英文:

Oracle doesn't have a time-only data type, but you can convert to a date, as you are attempting; if the value you have represents a time as the number of seconds past midnight then you can use the sssss format model:

to_date(to_number(transactiontime), 'sssss')

Your client will probably be configured to show just the date, or the full date time, so you might see 01-MAY-23 or 2023-05-01 11:48:19 or something else. (If you don't specify any date elements it defaults to the first day of the current month.) You can convert the date back to a string with to_char():

to_char(to_date(to_number(transactiontime), 'sssss'), 'HH24:MI:SS')

which will give 11:48:19.

You can also convert your number to an interval data type with:

numtodsinterval(to_number(transactiontime), 'SECOND') 

or

to_number(transactiontime) * interval '1' second

which both give +000000000 11:48:19.000000000.

You can't format that directly, but you could cut out the bit you want with:

substr(to_number(transactiontime) * interval '1' second, 12, 8)

fiddle

Or you could add that interval to a fixed date (at midnight), such as a date literal like `date '1970-01-01'; or - if you are for some reason storing your transaction date and time as two separate fields (which is rarely, if ever, sensible) - to that date. And you can then format the combined value however you want.

答案2

得分: -1

要将数字转换为日期,只需使用TO_DATE()函数,它会自动将其转换,并且您还可以提供输出所需的参数。

这是完整的格式和语法信息指南Oracle Dates

您可以使用以下查询,它会为您工作。当您需要虚拟表(行/列)数据集和系统时间等时,通常会使用"Dual"。

Select TO_DATE('42499', 'HH:MM:SS') FROM dual;
英文:

For converting a number to date to simply need TO_DATE() function that will automatically convert it and also you can provide parameters for the output which you want.

Here is the complete guide of formats and syntax information Oracle Dates

You can use this query that will work for you. "Dual" is basically used when you want a dummy table(row/column) dataset and systems time etc.

Select TO_DATE('42499', 'HH:MM:SS') FROM dual;

huangapple
  • 本文由 发表于 2023年5月31日 23:42:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定