Whats wrong in this query:
select *, STR_TO_DATE(start, '%d/%m/%Y') as date_format from dates where date_format >= 2018-03-19
error:
Column not found: 1054 Unknown column 'date_format' in 'where clause'
Whats wrong in this query:
select *, STR_TO_DATE(start, '%d/%m/%Y') as date_format from dates where date_format >= 2018-03-19
error:
Column not found: 1054 Unknown column 'date_format' in 'where clause'
You cannot use a column alias in a where
clause. MySQL has an extension where you can do so in a having
clause (without doing any aggregation). So you can do:
select d.*, STR_TO_DATE(start, '%d/%m/%Y') as date_format
from dates d
having date_format >= '2018-03-19';
The normal advice is to repeat the expression:
select d.*, STR_TO_DATE(start, '%d/%m/%Y') as date_format
from dates d
having STR_TO_DATE(start, '%d/%m/%Y') >= '2018-03-19';
However, I would strongly recommend that you change the structure of the table. The date should not be stored as a string. You can easily fix this:
update dates
set start = STR_TO_DATE(start, '%d/%m/%Y');
alter table dates modify column start date;
You can not use date_format as it is just given name but STR_TO_DATE(start, '%d/%m/%Y')
try this way
date_format
is not field so use start
instead of date_format
select *, STR_TO_DATE(start, '%d/%m/%Y') as date_format from dates where start >= 2018-03-19