I have a mysql table called test, the structure code is given below
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`assigned_date` date NOT NULL
) ENGINE=InnoDB
And the table is containing following data :
INSERT INTO `test` (`id`, `name`, `assigned_date`) VALUES
(0, 'A', '2012-12-23'),
(0, 'B', '2012-12-25');
Now the problem is when I run the following query it doesn’t return me any row.
SELECT name
FROM test
WHERE
DATE_FORMAT(assigned_date, '%m/%d/%Y') >= '12/01/2012'
AND
DATE_FORMAT(assigned_date, '%m/%d/%Y') <= '01/02/2013'
But When I use the following commands it returns me two rows as expected.
SELECT name
FROM test
WHERE
assigned_date >= STR_TO_DATE('12/01/2012', '%m/%d/%Y')
AND
assigned_date <= STR_TO_DATE('01/02/2013', '%m/%d/%Y')
What is the difference ? Why DATE_FORMAT didn’t work ?
Please Help with this I have used date_format function through out a site. Now it seems like I have to update the entire site.
Thanks.
DATE_FORMATconverts DATE to STRING.STR_TO_DATEconverts STRING to DATE.From MySQL Docs