1

嗨,我是 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
4

2 回答 2

2

您可以使用TO_DATE函数:

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

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

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

date '1981-11-17'

于 2013-07-18T04:12:11.737 回答
0

日期在 SQL 中是格式敏感的。因此,您需要将它们括在单引号中,例如 ('21-Dec-2010')。如果您的日期不是标准格式,那么您需要使用特定格式模型使用单行转换函数 TO_DATE 进行转换。您可以在我的门户网站http://www.suhelsayyad.com/p/blog-page_14.html上找到更多这些语法

于 2013-07-18T04:48:26.600 回答