简体   繁体   English

Oracle SQL Developer 中的 DATE 格式

[英]DATE format in Oracle SQL Developer

Just a small issue with some coding within Oracle SQL Developer.只是 Oracle SQL Developer 中某些编码的一个小问题。 This is my first time away from SQL Server and i'm already confused as to what I have got wrong.这是我第一次离开 SQL Server,我已经对我做错了什么感到困惑。

I'm just trying to insert some data into a table but i'm reaching an error that states;我只是想在表中插入一些数据,但我遇到了一个错误;
Error report - ORA-01861: literal does not match format string

I have the date field in my table set up as data type DATE.我将表中的日期字段设置为数据类型 DATE。 All I am trying to insert is some basic information;我想插入的只是一些基本信息;
INSERT INTO BasicUsers VALUES(2125, 'Dave', 'Forest', 'John', '1998-10-25', 30.54);

I have done some research on the internet and there is nothing solid for me to understand.我在互联网上做了一些研究,没有什么可以让我理解的。 I have tried altering the date format to YYYY-MON-DD so 1998-Oct-25 but that isn't working either.我尝试将日期格式更改为 YYYY-MON-DD 所以 1998-Oct-25 但这也不起作用。

Can anyone explain what I have done wrong here and how I can rectify this issue.谁能解释我在这里做错了什么以及我如何纠正这个问题。
Thank you.谢谢你。

'1998-10-25' is not a DATE data type; '1998-10-25'不是DATE数据类型; it is a string literal.它是一个字符串文字。

Instead, use a DATE literal:相反,使用DATE文字:

INSERT INTO BasicUsers  VALUES(2125, 'Dave', 'Forest', 'John', DATE '1998-10-25', 30.54);

Or use the TO_DATE function with a format model to explicitly convert from a string to a DATE :或者使用带有格式模型的TO_DATE函数将字符串显式转换为DATE

INSERT INTO BasicUsers
  VALUES(2125, 'Dave', 'Forest', 'John', TO_DATE( '1998-10-25', 'YYYY-MM-DD' ), 30.54);

Don't rely on implicit conversions from a string to a DATE as Oracle will implicitly try to convert the date to a string using the NLS_DATE_FORMAT session parameter.不要依赖从字符串到DATE隐式转换,因为 Oracle 将尝试使用NLS_DATE_FORMAT会话参数隐式地将日期转换为字符串。 Your query is effectively:您的查询有效:

INSERT INTO BasicUsers
  VALUES(2125, 'Dave', 'Forest', 'John',
  TO_DATE(
    '1998-10-25',
    ( SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_DATE_FORMAT' )
  ),
  30.54);

If the session parameter does not match then your query will raise an exception.如果会话参数不匹配,那么您的查询将引发异常。 Since the user can change their session parameters at ANY time then you can never rely on this format to be consistent between users or even user's sessions so you should always be explicit about the conversions that you are performing.由于用户可以随时更改自己的会话参数,那么你就可以永远依靠这种格式是用户甚至用户的会话之间是一致的,所以你应该始终明确的转换,你的执行情况。

Use the DATE prefix:使用DATE前缀:

INSERT INTO BasicUsers 
    VALUES (2125, 'Dave', 'Forest', 'John', DATE '1998-10-25', 30.54)

I would also advise you to include explicit column names for the insert.我还建议您为插入包含明确的列名。 For instance, the values could be defined in a different order from how you are providing the values.例如,值的定义顺序可能与您提供值的顺序不同。

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

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