如何插入日期?

时间:2013-07-18 04:05:23

标签: oracle

您好我是Oracle新手并忘记了如何输入日期。我尝试了很多变化,似乎无法得到它,而且我已经在互联网上看了但是找不到正确的例子。我的表设置如下:hiredate DATE,(DATE是我假设默认输入,但默认值是什么)。

感谢您的帮助。

这是我的代码:

SQL> SET LINESIZE 120
SQL> SET PAGESIZE 45
SQL> SET FEEDBACK 1
SQL> SET ECHO ON
SQL> 
SQL> DROP TABLE employee
  2  
SQL> CREAT TABLE student
SP2-0734: unknown command beginning "CREAT TABL..." - rest of line ignored.
SQL> CREATE TABLE employee
  2  (
  3  empno    NUMBER(4)CONSTRAINT employee_empno_PK PRIMARY KEY,
  4  empname  VARCHAR(10),
  5  job      VARCHAR(10),
  6  manager  NUMBER(4),
  7  hiredate DATE,
  8  salary   NUMBER(7,2),
  9  commission NUMBER(7,2),
 10  deptno   NUMBER(2)
 11  );

Table created.

SQL> INSERT INTO employee
  2   VALUES(7839, 'President', NULL, 11/17/1981, 5000, NULL, 10);
INSERT INTO employee
            *
ERROR at line 1:
ORA-00947: not enough values


SQL> VALUES(7839, 'President', NULL, '11/17/1978', 5000, NULL, 10);
SP2-0734: unknown command beginning "VALUES(783..." - rest of line ignored.
SQL> INSERT INTO employee
  2   VALUES(7839, 'President', NULL, '11/17/1978', 5000, NULL, 10);
INSERT INTO employee
            *
ERROR at line 1:
ORA-00947: not enough values


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, 11/17/1981, 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, 11/17/1981, 5000, NULL, 10)
                                              *
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, 19811117, 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, 19811117, 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-00932: inconsistent datatypes: expected DATE got NUMBER


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '11/17/1981', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '11/17/1981', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01843: not a valid month


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '1978/11/17
  3  )
  4  ;
ERROR:
ORA-01756: quoted string not properly terminated


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President' NULL, '1978/11/17', 5000, NULL, 10);
 VALUES(7839, 'King', 'President' NULL, '1978/11/17', 5000, NULL, 10)
                                  *
ERROR at line 2:
ORA-00917: missing comma

Hello, I am new to MySQL and forgot how to properly INSERT at DATE when using MySQL Plus. I have tried many different variations as you will be able to see and I can't seem to figure it out. I also looked on the internet but always find examples where they make there own formats. What is the correct way to insert a date when the table was created using: hiredate


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '1981/11/17', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '1981/11/17', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01861: literal does not match format string


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '1981/17/11', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '1981/17/11', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01861: literal does not match format string


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01843: not a valid month


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01847: day of month must be between 1 and last day of month


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 100);
 VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 100)
                                         *
ERROR at line 2:
ORA-01843: not a valid month


SQL> INSERT INTO employee
  2   VALUES(7839, 'King, 'President', NULL, '11/17/1981', 5000, NULL, 10);
ERROR:
ORA-01756: quoted string not properly terminated


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '11/17/81', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '11/17/81', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01843: not a valid month


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '17/11/81', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01843: not a valid month


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '81/17/11', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '81/17/11', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01847: day of month must be between 1 and last day of month


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '17/81/11', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '17/81/11', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01843: not a valid month


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '81/11/17', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01847: day of month must be between 1 and last day of month


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '1981-11-17', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '1981-11-17', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01861: literal does not match format string


SQL> INSERT INTO employee
  2   VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10);
 VALUES(7839, 'King', 'President', NULL, '17/11/1981', 5000, NULL, 10)
                                         *
ERROR at line 2:
ORA-01843: not a valid month

2 个答案:

答案 0 :(得分:1)

您可以使用TO_DATE功能:

TO_DATE('11/17/1981', 'MM/DD/YYYY')

它允许您将日期指定为字符串,然后将其格式指定为Oracle,以便将其转换为日期。

或者你可以使用日期文字:

date '1981-11-17'

答案 1 :(得分:0)

日期在SQL中格式敏感。所以你需要用单引号括起来 (' 21日 - 12月2010&#39)。如果您的日期不是标准格式,则需要使用特定格式模型使用单行转换函数TO_DATE进行转换。您可以在我的门户网站上找到更多这些语法 http://www.suhelsayyad.com/p/blog-page_14.html