# comparing datetime with string is not using index

**URL:** <https://forums.percona.com/t/comparing-datetime-with-string-is-not-using-index/1820>\
**Category:** Other MySQL® Questions\
**Created:** [June 5, 2012, 1:39pm UTC](https://forums.percona.com/t/comparing-datetime-with-string-is-not-using-index/1820 "2012-06-05T13:39:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![perforum111](https://avatars.discourse-cdn.com/v4/letter/p/aca169/32.png) [@perforum111](https://forums.percona.com/u/perforum111)\
**Post date:** [June 5, 2012, 1:39pm UTC](https://forums.percona.com/t/comparing-datetime-with-string-is-not-using-index/1820/1 "2012-06-05T13:39:36Z")

</div>

Not really sure if this is a bug but it looks like one… I have a log table with a datetime field on it that is indexed, running a query that tries to compare that field with a string will not use index:

EXPLAIN SELECT \* FROM `log_activities` WHERE `activityDate` = ‘2012-06-01 13:47:31’;  
id select\_type table type possible\_keys key key\_len ref rows Extra  
1 SIMPLE log\_activities ALL NULL NULL NULL NULL 162069 Using where

what I just realised is that casting this to datetime will result in index usage  
EXPLAIN SELECT \* FROM `log_activities` WHERE `activityDate` = cast(‘2012-06-01 13:47:31’ as datetime);  
id select\_type table type possible\_keys key key\_len ref rows Extra  
1 SIMPLE log\_activities ref activityDate activityDate 8 const 1 Using where

my q is is this a bug or intended? …should I report it somewhere (and where)?

PS: found something similar in mysql bugs  
[URL][MySQL Bugs: #52849: datetime index not work](http://bugs.mysql.com/bug.php?id=52849%5B/URL%5D)  
…in my tables I am using utf8\_unicode\_ci as collation
