Oracle 中如何插入日期时间类型的数据,首先为了演示, 新建数据表如下
create table t( mydate date);
插入日期时间
SQL> insert into t values(to_date('2015-1-30 12:20:33','YYYY-MM-DD HH24:MI:SS'));
查询日期时间
SQL> select to_char(mydate,'YYYY-MM-DD HH24:MI:SS') from t;
参数忽略时的情景
当省略HH、MI和SS对应的输入参数时,Oracle使用00作为DEFAULT值。
如果输入的日期数据忽略时间部分,Oracle会将时、分、秒部分都置为0,也就是说会取整到日。
同样,忽略了DD参数,Oracle会采用1作为日的默认值,也就是说会取整到月。
但奇怪的是使用select *from t,只是查询到年月日的信息,时分秒的数据看不到。
SQL> select * from t;
MYDATE
--------------
30-1月 -15
30-1月 -15
30-1月 -15
30-1月 -15
01-1月 -15
01-1月 -15
已选择 6 行。
而使用select to_char(mydate,'YYYY-MM-DD HH24:MI:SS') from t;格式化只有就有了
-------------------
2015-01-30 12:20:33
2015-01-30 12:20:00
2015-01-30 13:00:00
2015-01-30 00:00:00
2015-01-01 00:00:00
2015-01-01 00:00:00
下面给出一个具体的例子,如何在项目中插入日期时间。备注,如果你定义的字段名,和Oracle数据库中的关键字重复时,可以选择在其上加入“”用于避免出现错误。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAz8AAAAjCAIAAAA1yyNpAAAJXUlEQVR4nO2dPW/cOBCG9dP9W1LmcNU16ZLKxQFBGhcBfIAb44AtAhjXpOQV8src+eJwSEmU9n1BLGSKMxxSJPVI2pWnP/78CwkJCQkJCQkJ6ShpShAEQRAEQdBxBHqDIK++/fMtkPaOGoIgCDqbQG8dNE0Pe4cAbaG+9DZN0/LZV+t5hhqFtQKCoC76WN+nhz2XlTVqn6ZpOYFNTLYVKWYYzstxrZVW1xoRkt7ghloZra6qVnhyPC3VMvmG4cfTGx4/hM/yHNJSjd6mW8DS+kcM1bDinoWqHx6WRPJJGVKYZBr9owVp9yopwI8X7x/bs6ftWut4F/H+Ea3s6hIADoKgHtri6rwXmQX8TIxR+IZowj9Fq2UhrrLSglw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d643uHojexCfb5sZHetEpLJh3ojbeLN/Mw9JYYYzXeexNqGu/JaUsthz0DvW+A3sQcbS/ozZMDetNMYvfemDd9dVWOS5cnp7wW0Fsgh+8dh978Z0y+q7bPG49Xx3tvntgC2ofe6D7HP13Wvmti341To7k2W0Ljih7xDDiePIG9/2l+U1DrMbEPN/OcbnvVftPHnGLvttDePKJd7oyUo/aY2C7eP55eXd7LsCTP+0F4XXmQPOy9coq9etC5c823vsFmmOQlxVtrni/DmYEJS5k2brUxRoYld57naAGYXSGUcYyfblYsHs8pqc/awt0WP41g8kPsqcsoXzwVGodejFz0I3ZOVSuuJR+MP01D6wA52+6raCN0S+k4b+vtS2/5tufwrHdI9vK8Gb0dSEaPjUxvg+t8c2dfVcXWhd5YAB1aMYJGPsoQl3R1PdYR3DieY0zEqoskLaVs3am6VDriSaLoGfRGVOwx0FtAp5w7ihVV98BiitHb6TXaiR+qFY7gKEsMtL1Ab1Uait4gyCnQGwSdUqC3+9UG9HYyDUJvEFSlWnqDIGh8gd7uWqvS2ym1O71BUEB+eoMg6BACvUEQBEEQBB1JoDcIgiAIgqAjCfQGQRAEQRB0JIHeIAiCIAiCjiTQGwRBEARB0JH0P+SiznnlILglAAAAAElFTkSuQmCC" alt="" />
参考文章
1. 理央silence, oracle日期的插入操作.