将 TIME 插入 Oracle SQL、DATE 字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15045600/
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
Insert TIME into Oracle SQL, DATE field
提问by Hyman
Is there anyway to insert only time (no date) into a SQL field of type 'Date'
无论如何只能将时间(无日期)插入到“日期”类型的 SQL 字段中
I have tried inserting:
我试过插入:
13:00
and
和
01:00
and
和
13:00pm
and
和
01:00pm
But keep getting the error:
但不断收到错误:
Not A Valid Month
回答by Gordon Linoff
The problem is the literal. For date/time values, Oracle expects values in the form ddMMMyyyy. (Or, you can use the expression DATE '2001-01-01
. Literal values are explained here.) There doesn't appear to be a default format for time without a date.
问题是字面意思。对于日期/时间值,Oracle 需要 ddMMMyyyy 形式的值。(或者,您可以使用表达式DATE '2001-01-01
。此处解释了文字值。)没有日期的时间似乎没有默认格式。
In other words, you can get the same error with cast('1/1/2001' as date)
.
换句话说,您可能会遇到相同的错误cast('1/1/2001' as date)
。
Instead, use the to_date()
function:
相反,使用该to_date()
函数:
select to_date('10:00', 'hh:mi')
from dual
This gives you much more flexibility with formats.
这为您提供了更大的格式灵活性。
Also, as Ben notes in the comments, this gives you the current date with the time specified.
此外,正如 Ben 在评论中指出的那样,这将为您提供指定时间的当前日期。
回答by Art
Example of using EXTRACT and time only portion of a SYSDATE and some date...:
使用 SYSDATE 和某个日期的 EXTRACT 和仅时间部分的示例...:
SELECT hh||':'||mi||':'||ss time_only_portion
FROM
(
Select
EXTRACT(hour From Cast(SYSDATE as timestamp)) hh,
EXTRACT(minute From Cast(SYSDATE as timestamp)) mi,
EXTRACT(second From Cast(SYSDATE as timestamp)) ss
From dual
)
/
SELECT EXTRACT(HOUR From t_stamp) hh
FROM
(
SELECT CAST(to_date('21/02/2012 06:10:00 am', 'dd/mm/yyyy hh:mi:ss am') AS TIMESTAMP) t_stamp
FROM dual
)
/