Skip to content
Advertisement

When selecting data with DATE_FORMAT() is not getting proper values MySQL

I’m having a table structure as below.

insert statements :

when i select with below query I’m not getting the proper out.

Output :

I need a data where dt values are less than 90 Days. Can anyone suggest where to make changes.

Advertisement

Answer

You are overcomplicating things here I think. Basically you are turning the dates to strings in format dd-mm-yyyy, and the comparing the strings. This does not do what you want. Typically, string 31-12-2020 is greater than 01-01-2021, (because the former starts with 3, and the latter with 0), while the corresponding dates compare the other way around.

You have a datetime column, so just use arithmetics:

On top of being correct, this is also much more efficient than performing conversions on the column being compared (eg: it may take advantage of an index on dt).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement