2013-07-22 22 views
5

ho creato una tabella nel mio database:SQL DML: valore di data non corretta (MySQL)

CREATE TABLE official_receipt(
    student_no INT UNSIGNED, 
    academic_year CHAR(8), 
    trimester ENUM('1', '2', '3'), 
    or_no MEDIUMINT UNSIGNED, 
    issue_date DATE NOT NULL, 
    received_from VARCHAR(255) NOT NULL, 
    amount_of DECIMAL(8,2) NOT NULL, 
    issued_by VARCHAR(255), 
    doc_type ENUM('FULL', 'DOWN', 'INST') NOT NULL, 
    form_of_payment ENUM('CASH', 'INST') NOT NULL, 
    PRIMARY KEY (student_no, academic_year, trimester, or_no) 
); 

ho inserito alcuni valori:

INSERT INTO official_receipt(student_no , academic_year, trimester, or_no, issue_date, received_from, amount_of, issued_by, doc_type, form_of_payment) 
VALUES 
    (201201121, 'AY201314', '1', 029940, 2013-05-21, 'NAME', 20000.00, NULL, 'DOWN', 'INST'), 
    (201201121, 'AY201314', '1', 029944, 2013-07-23, 'NAME', 8000.00, NULL, 'INST', 'INST'), 
    (201201101, 'AY201314', '1', 029941, 2013-05-21, 'NAME', 56650.00, NULL, 'FULL', 'CASH'), 
    (201201037, 'AY201314', '1', 029942, 2013-05-21, 'NAME', 56650.00, NULL, 'FULL', 'CASH'), 
    (201201142, 'AY201314', '1', 029943, 2013-05-21, 'NAME', 63800.00, NULL, 'FULL', 'CASH'); 

sto ottenendo questo errore:

Error Code: 1292. Incorrect date value: '1987' for column 'issue_date' at row 1 

Sono abbastanza perplesso perché ho già seguito il formato AAAA-MM-GG. Qualsiasi aiuto?

risposta

7

Come documentato sotto Date and Time Literals:

MySQL recognizes DATE values in these formats:

  • As a string in either 'YYYY-MM-DD' or 'YY-MM-DD' format. A “relaxed” syntax is permitted: Any punctuation character may be used as the delimiter between date parts. For example, '2012-12-31' , '2012/12/31' , '2012^12^31' , and '[email protected]@31' are equivalent.

  • As a string with no delimiters in either 'YYYYMMDD' or 'YYMMDD' format, provided that the string makes sense as a date. For example, '20070523' and '070523' are interpreted as '2007-05-23' , but '071332' is illegal (it has nonsensical month and day parts) and becomes '0000-00-00' .

  • As a number in either YYYYMMDD or YYMMDD format, provided that the number makes sense as a date. For example, 19830905 and 830905 are interpreted as '1983-09-05' .

Pertanto, l'espressione 2013-05-21 non è una data MySQL valida letterale (è infatti un'espressione aritmetica, composto da due sottrazioni: il risultato è il numero intero 1987). Per rispettare uno dei formati letterali sopra descritti, è necessario citare il valore letterale della data come stringa e/o rimuovere i delimitatori.

+0

Grazie mille. –

+0

È a malapena evidente che stai parlando di citazioni mancanti qui. – ebyrob

+0

@ebyrob: Questo perché non lo sono. C'è un formato letterale perfettamente valido per le date che non richiede quotazioni: in questo caso è sufficiente rimuovere i delimitatori. – eggyal

4

Ti manca con ' singole virgolette attorno ai valori issue_date per la mia prova inserisce i record con successo

Prova questa

INSERT INTO official_receipt(student_no , academic_year, trimester, or_no, issue_date, received_from, amount_of, issued_by, doc_type, form_of_payment) 
VALUES 
    (201201121, 'AY201314', '1', 029940, '2013-05-21', 'NAME', 20000.00, NULL, 'DOWN', 'INST'), 
    (201201121, 'AY201314', '1', 029944, '2013-07-23', 'NAME', 8000.00, NULL, 'INST', 'INST'), 
    (201201101, 'AY201314', '1', 029941, '2013-05-21', 'NAME', 56650.00, NULL, 'FULL', 'CASH'), 
    (201201037, 'AY201314', '1', 029942, '2013-05-21', 'NAME', 56650.00, NULL, 'FULL', 'CASH'), 
    (201201142, 'AY201314', '1', 029943, '2013-05-21', 'NAME', 63800.00, NULL, 'FULL', 'CASH'); 

Here is your fiddle

+0

Grazie mille. Anche quell'app Web è molto utile. –

2

Hai bisogno di mettere la data letterale tra virgolette . Il messaggio di errore dice 1987 perché la data non quotata viene letta come l'espressione 2013 minus 5 minus 21, che è 1987.

Le tue date possono essere le seguenti: '2013-05-21' o '20130521' o un paio di altri formati coperti nello documentation.

+0

Grazie mille. –