定义oracle数据库的日期格式

发布于 2024-10-16 11:50:16 字数 692 浏览 2 评论 0原文

我正在尝试执行以下 SQL:

INSERT INTO "x" 
   ("x_id", "requested_function", "user_action", "t_users_id", "date", "wf_process", "details") 
VALUES 
   ('66', 'index', 'view', '1', '2011-02-04 22:14:19', '15', '');

..但我收到此错误:ORA-01861:文字与格式字符串不匹配。

如果我像这样添加日期格式,这个错误就解决了:

INSERT INTO "x" 
  ("x_id", "requested_function", "user_action", "t_users_id", "date", "wf_process", "details") 
VALUES 
  ('66', 'index', 'view', '1',to_date('2011-02-04 22:14:19','yyyy-mm-dd hh24:mi:ss'), '15', '');

我不想为每个SQL语句定义日期格式,我希望这种格式成为数据库中所有日期字段的标准格式,而不需要定义它,在换句话说,第一个 SQL 语句必须正确运行,没有任何错误。

我可以为oracle数据库定义日期格式,这样就不需要为每个SQL语句再次定义它吗?

I'm trying to execute the following SQL:

INSERT INTO "x" 
   ("x_id", "requested_function", "user_action", "t_users_id", "date", "wf_process", "details") 
VALUES 
   ('66', 'index', 'view', '1', '2011-02-04 22:14:19', '15', '');

..but i get this error : ORA-01861: literal does not match format string.

This error solved if i add the date format like this:

INSERT INTO "x" 
  ("x_id", "requested_function", "user_action", "t_users_id", "date", "wf_process", "details") 
VALUES 
  ('66', 'index', 'view', '1',to_date('2011-02-04 22:14:19','yyyy-mm-dd hh24:mi:ss'), '15', '');

I don't want to define the date format for each SQL statement, I want this format to be standard for all date fields in the database, without the need to define it,in other word the first SQL statement must run correctly without any errors.

Can I define the date format for oracle database, so no need to define it again with each SQL statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云柯 2024-10-23 11:50:16

这是思考编程的错误方式。隐式的不好,显式的好。如果您只是将字符串放入数据列中并希望默认值按您的预期工作,则很可能会产生错误和性能问题。

但我绝不会妨碍得到问题的答案。

更改 NLS_DATE_FORMAT 初始化参数。

That is the wrong way to think about programming. Implicit is bad, explicit is good. If you just toss a string into a data column and hope the default works as you expect, you're very likely to create bugs and performance issues.

But far be it from me to get in the way of getting the answer to the question.

Change the NLS_DATE_FORMAT initialization parameter.

不…忘初心 2024-10-23 11:50:16

这是非常糟糕的 SQL。两个问题:1)Oracle将无法优化(每次运行查询时都必须解析),使得数据库操作变慢; 2) 您容易受到 SQL 注入攻击。

最好将参数化查询与准备好的语句一起使用,这将解决上述问题以及日期格式问题。

编辑:要直接回答你的问题,如果你真的不想改用准备好的语句(你已经被警告过!),那么你可以做这样的事情:

INSERT INTO X VALUES (99, TO_DATE('2010/02/04 16:30:00', 'YYYY/MM/DD HH24:MI:SS'));

但同样,这是一个非常由于上述原因,这是个坏主意

That is pretty bad SQL. Two problems: 1) Oracle will not be able to optimize (will have to parse each time you run a query), making database operations slow; and 2) You are open to SQL injection attacks.

It's far better to use parametrized queries with prepared statements, which will solve both issues above and your date formatting problem as well.

EDIT: To directly answer your question, and if you really do not want to change over to prepared statements (you have been warned!), then you can do somethign like this:

INSERT INTO X VALUES (99, TO_DATE('2010/02/04 16:30:00', 'YYYY/MM/DD HH24:MI:SS'));

But again, it's a very bad idea for the reasons stated above

旧夏天 2024-10-23 11:50:16

日期和时间文字的 SQL92 标准就是此处的方法。

TIMESTAMP 'YYYY-MM-DD HH:MM:SS'
DATE 'YYYY-MM-DD'
TIME 'HH:MM:SS'

使用它的一个例子是:

INSERT INTO X VALUES (99, TIMESTAMP '2010-02-04 16:30:00');

The SQL92 standard for date and time literals is the way to go here.

TIMESTAMP 'YYYY-MM-DD HH:MM:SS'
DATE 'YYYY-MM-DD'
TIME 'HH:MM:SS'

An example of using it would be:

INSERT INTO X VALUES (99, TIMESTAMP '2010-02-04 16:30:00');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文