Tuesday 15 January 2013

mysql date direct subtraction -


I have two date columns, date A and dateB . If I decrease the dateA to dateB ( dateB - date A ), then I get the wrong result but DATEDIFF (dateB, DateA) do not work . When using data in the table, I do not get the result below with direct subtraction.

SELECT DATE ('2013-01-31') - DATE ('2013-01-27 /

Why?

Edit:

I found that if the two dates are within one month in MySQL, the direct subtraction gives the right result, but if the duration of the month, the problem can be a problem.

Is that right?

Probably, you are not stored in the form of dates, But in the form of wire. If they are stored in the form of dates, The subtraction will work as expected.

When you decrease the two strings, like:

  SELECT '2013-01-31' - '2013-01-27'   

Then, MySQL converts them into numbers based on major digits. In this case, both numbers start with 2013 , so both 2013 (If there were no digits in the beginning, the value would be 0 ). These numbers are reduced.

No comments:

Post a Comment