-1

Why does the below condition work? Does MySQL convert multiple dashes '---' to a single '-' dash. I tried finding an explanation for this but I couldn't find anything.

WHERE DATE(login_at) = '2019---04---30'

Can anyone explain to me how this works?

I'm using MySQL Version : 5.7.18

Vishal
  • 639
  • 7
  • 32

1 Answers1

2

MySQL allows a flexibility, by auto-interpreting various delimiters, and converts them to proper date[time] format. It is well explained in documentation:

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 :, 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 valid month.

The only delimiter recognized between a date and time part and a fractional seconds part is the decimal point.

So, in your case, MySQL is considering the delimiter as ---, and thus implicitly converting to Y-m-d format.

However, it is always better and safe to use the standard Y-m-d h:i:s format, to represent a date/datetime value.

Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57