Sumit Aggarwal
Sumit Aggarwal

Reputation: 841

How to select the filed as date and time

I have data in end_date field like 29 August 2016 - 18:45.

I want to write a query where end_date matches the current server date time.

I am using this

select * from round_dates where end_date = CURDATE()

CURDATE() matches the today date but I need to match the time also

Upvotes: 0

Views: 39

Answers (3)

Ashu Jha
Ashu Jha

Reputation: 344

NOW() return the current datetime stamp of Mysql Server. Whereas CURDATE() returns the date stamp only.

So, you can use this below Query

SELECT * FROM table_name WHERE STR_TO_DATE(start_date, '%e %M %Y - %k:%i') = NOW()

Upvotes: 1

Mohamed Yasin
Mohamed Yasin

Reputation: 440

select *
from round_dates
where end_date =  DATE_FORMAT(now(),'%e %M %Y - %k:%i')

Upvotes: 2

Shadow
Shadow

Reputation: 34232

Use MySQL's str_to_date() or date_format() functions to convert either the stored data to date format or the date format to the currently stored format.

Side note: I would rather do a one-off exercise of converting that field's data to standard MySQL datetime format, than trying to fix any queries relying on this field.

...where str_to_date(end_date,'%e %M %Y - %k:%i')=now()

Upvotes: 0

Related Questions