Click here to Skip to main content
15,914,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello , i use mysql
when i insert wrong value in a date column
it inserted as 0000-00-00 instead of showing me an error
for example : insert 'hello' in birth date column

i need when i do a behavior like that and try to insert wrong values in date columns
i need my sql to show me error that tell me i am insert wrong data ,and insertion operation not happened .
i don't need to insert 0000-00-00
i try
set_mode='NO_ZERO_DATE'

but still same problem
Posted
Updated 15-Feb-15 2:31am
v2
Comments
Kuthuparakkal 15-Feb-15 8:41am    
What is the column data type in mysql table ?
Hercal 15-Feb-15 8:49am    
datetime

1 solution

According to the MySQL Documentation: [^]
Quote:
Be aware of certain properties of date value interpretation in MySQL:

MySQL permits a “relaxed” format for values specified as strings, in which any punctuation character may be used as the delimiter between date parts or time parts. In some cases, this syntax can be deceiving. For example, a value such as '10:11:12' might look like a time value because of the “:” delimiter, but is interpreted as the year '2010-11-12' if used in a date context. The value '10:45:15' is converted to '0000-00-00' because '45' is not a legal month.

As of 5.0.2, the server requires that month and day values be legal, and not merely in the range 1 to 12 and 1 to 31, respectively. With strict mode disabled, invalid dates such as '2004-04-31' are converted to '0000-00-00' and a warning is generated. With strict mode enabled, invalid dates generate an error. To permit such dates, enable ALLOW_INVALID_DATES. See Section 5.1.7, “Server SQL Modes”, for more information.

Visit Server SQL Modes[^] to find out how to set to strict mode.
However, if the query is coming from an application, the date validation should be done at the coding side. Or you may consider using a datapicker.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900