sam sexton
sam sexton

Reputation: 193

How to select from MYSQL database WHERE its only from two DATETIME?

I have a tabel in MYSQL database that has 2 columns.

1- start_date
2- end_date

the above columns both hold DATETIME like so:

start_date                  end_date

2019-10-03 13:30:00     2020-10-03 23:30:00 

I need to select from the MYSQL table ONLY if the current date is between the start_date and end_date.

So i tried this MYSQL Query:

SELECT * FROM table WHERE `end_date` >= CURDATE() AND `start_date` <= CURDATE()

but this doesn't work and doesn't return anything from the database even thogh the the current date is between the start_date and end_date.

Could someone please advice on this?

Upvotes: 1

Views: 56

Answers (3)

Dilip Hirapara
Dilip Hirapara

Reputation: 15296

Q: I need to select from the MYSQL table ONLY if the current date is between the start_date and end_date.

Try this.

SELECT * FROM `table` WHERE CURDATE() between start_date and end_date

Convert it with date before because you're using their timestamp.

SELECT * FROM `table` WHERE CURDATE() between date(start_date) and date(end_date)

Or

SELECT * FROM `table` WHERE NOW() between start_date and end_date

Upvotes: 1

Veerender Mishra
Veerender Mishra

Reputation: 88

Using SQL date function in query

SELECT * FROM user WHERE date(end_date) >= CURDATE() AND date(start_date) <= CURDATE()

Upvotes: 1

Krishna Jangid
Krishna Jangid

Reputation: 5410

try this

SELECT * FROM table 
WHERE start_date >= '2019-10-03 13:30:00' 
AND end_date <= '2020-10-03 23:30:00'

Upvotes: 0

Related Questions