简体   繁体   中英

Parsing the date with timezone MySQL

In a table say testTable we are storing date-time as varchar in following format

Tue May 09 15:16:54 IST 2017

I am trying to write a query which gives me all records between two dates using STR_TO_DATE to convert the date in varchar format to datetime . However below query is failing with Error Code: 1241 Operand should contain 1 column(s)

SELECT * FROM test12.testTable a WHERE a.timestamp BETWEEN (STR_TO_DATE('Tue May 09 17:26:11 IST 2017', '%a %b %d %H:%i:%s %Z %Y'), 
  STR_TO_DATE('Wed May 10 20:17:11 IST 2017', '%a %b %d %H:%i:%s %Z %Y'));

Could you suggest what is wrong here?

When your dates are stored in this weird way, you want to use the STR_TO_DATE() function on the column, not on the string you provide.

SELECT * FROM test12.testTable a 
WHERE STR_TO_DATE(a.timestamp, '%a %b %d %H:%i:%s %Z %Y') 
BETWEEN '2017-05-09 17:26:11' AND '2017-05-10 20:17:11';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM