简体   繁体   English

如何在 SQL 日期类型列中插入日期?

[英]How to insert date in SQL date type column?

I have a table with date type column.我有一个带有日期类型列的表。

在此处输入图片说明

I am trying to insert date in it:我正在尝试在其中插入日期:

在此处输入图片说明

But I get an error:但我收到一个错误:

在此处输入图片说明

Please give me to make the correct query to put the date请给我做正确的查询以放置日期

轻松修复::

INSERT INTO t(dob) VALUES(DATE '2015-12-17');

Assuming this is an Oracle question based on the ORA-01843 error message , the problem appears to be in the date formatting as the error suggests.假设这是一个基于ORA-01843 错误消息的 Oracle 问题,问题似乎出现在错误提示的日期格式中。

In the provided example does the date '6-3-2012' mean '3 March 2012' or 'June 6, 2012?'在提供的示例中,日期“6-3-2012”是指“2012 年 3 月 3 日”还是“2012 年 6 月 6 日?” The answer lies within the NLS_DATE_FORMAT parameter.答案就在NLS_DATE_FORMAT参数中。

Out of the box, the Oracle date format is DD-MON-RR.开箱即用的 Oracle 日期格式是 DD-MON-RR。 So your corrected date format is either '03-MAR-12' or '06-JUN-12.'因此,您更正的日期格式是“03-MAR-12”或“06-JUN-12”。 If the NLS_DATE_FORMAT has not been changed.如果 NLS_DATE_FORMAT 尚未更改。

Never try to insert a string into a date column!切勿尝试将字符串插入日期列! If you have a string, use the to_date function with an explicit date format (and use 4 digit dates).如果您有一个字符串,请使用具有显式日期格式的to_date函数(并使用 4 位日期)。

Relying on nls_date_format to implicitly convert your strings is just asking for trouble (like you just did), it can very easily change, even some apps will change it themselves.依靠nls_date_format隐式转换你的字符串只是自找麻烦(就像你刚刚做的那样),它可以很容易地改变,甚至一些应用程序会自己改变它。

The date literal ( date '2015-12-17' https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm#BABGIGCJ ) always uses the same date format so that might be okay for ad hoc statements but you need to be aware that it is literal by name and literal by nature.日期文字( date '2015-12-17' https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements003.htm#BABGIGCJ )始终使用相同的日期格式,因此这可能适用于临时声明,但您需要注意它的名称和性质都是字面意思。 They don't support bind variables so you will end up writing unshareable SQL to chew up your shared pool.它们不支持绑定变量,因此您最终将编写不可共享的 SQL 来占用您的共享池。

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

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