简体   繁体   English

在 Oracle SQL 开发人员中,您如何插入日期和时间

[英]In Oracle SQL developer how do you insert date and time

I am unable to insert date data and time data into an Oracle table.我无法将日期数据和时间数据插入 Oracle 表中。 Please find the below query and error请找到以下查询和错误

INSERT INTO Sunday_Service (Service_ID, Service_date, 
                            Start_time, End_time, 
                            Service_location, No_of_children)
VALUES (Seq_service_id.nextVal, TO_DATE('YYYY-MM-DD', '2022-07-03'), 
        TO_DATE('hh24:mi:ss', '09:00:00'), TO_DATE('hh24:mi:ss', '10:15:00'), 
        'RM 2101', '10');

Error:错误:

错误报告 ORA-01821

You have your parameters switched in your TO_DATE function calls.您在TO_DATE function 调用中切换了参数。 Your TO_DATE function calls should look like this:您的TO_DATE function 调用应如下所示:

TO_DATE ('2022-07-03', 'YYYY-MM-DD'),
TO_DATE ('09:00:00', 'hh24:mi:ss'),
TO_DATE ('10:15:00', 'hh24:mi:ss')

You have the arguments to to_date() the wrong way around:你有 arguments to_date()错误的方式:

INSERT INTO Sunday_Service (Service_ID, Service_date, 
                            Start_time, End_time, 
                            Service_location, No_of_children)
VALUES (Seq_service_id.nextVal,
        TO_DATE('2022-07-03', 'YYYY-MM-DD'), 
        TO_DATE('09:00:00', 'hh24:mi:ss'),
        TO_DATE('10:15:00', 'hh24:mi:ss'), 
        'RM 2101',
        '10');

But you probably want to combine the time and date, rather than holding them in separate columns;但是您可能希望将时间和日期结合起来,而不是将它们放在单独的列中; so if you removed service_date from your table you could do:所以如果你从你的表中删除了service_date你可以这样做:

INSERT INTO Sunday_Service (Service_ID,
                            Start_time, End_time, 
                            Service_location, No_of_children)
VALUES (Seq_service_id.nextVal,
        TO_DATE('2022-07-03 09:00:00', 'YYYY-MM-DD HH24:MI:SS'), 
        TO_DATE('2022-07-03 10:15:00', 'YYYY-MM-DD HH24:MI:SS'), 
        'RM 2101',
        '10');

Apart from anything else, that will make it possible to handle service calls that span midnight or multiple days.除此之外,这将使处理跨越午夜或数天的服务呼叫成为可能。

You could also use timestamp literals:您还可以使用时间戳文字:

...
VALUES (Seq_service_id.nextVal,
        TIMESTAMP '2022-07-03 09:00:00', 
        TIMESTAMP '2022-07-03 10:15:00', 
...

or slightly more explcitly:或更明确地说:

...
VALUES (Seq_service_id.nextVal,
        CAST(TIMESTAMP '2022-07-03 09:00:00' AS DATE), 
        CAST(TIMESTAMP '2022-07-03 10:15:00' AS DATE), 
...

If no_of_children is a number column, as it appears, then the last value should be a number - 10 rather than '10' .如果no_of_children是一个数字列,就像它出现的那样,那么最后一个值应该是一个数字 - 10而不是'10'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM