1

I have a datetime field and want to get all entries before 11am. It is for an online store where you can order until 11:00am and it will be shipped the same day.

SELECT * 
FROM Tablename 
WHERE (order.date_added < STR_TO_DATE(YEAR(), MONTH(), DAY()'%Y %d,%M'), '11:00:00')

Unfortunately, I do not know how to assemble this statement.

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74

2 Answers2

1

If you want orders before 11:00 a.m. today, you can use:

where o.date_added >= curdate() and
      time(o.date_added) < '11:00:00'
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • I thank you for your answer exactly what I was looking for. now I can add 5 more minutes so everyone is satisfied and gets his order. Thanks for your help. `SELECT * FROM order WHERE order.date_added >= curdate() and time(order.date_added) < '11:05:00'` – Alf Müller Jul 11 '21 at 10:34
0

This gives you the currentday at 11 o'clock

CurDATE() + INTERVAL  11 HOUR

And the query selects all orders between yesterday 11 and today eleven.

you should add also criteria to exclude all orders that are already made

CREATE tABLe orders (date_added DATETIME)
INSERT INTO orders VALUES (NOW() -INTERVAL 10 HOUR)
SELECT * 
FROM orders 
WHERE orders.date_added BETWEEN (CurDATE() - INTERVAL 1 DAY) + INTERVAL  11 HOUR AND  CurDATE() + INTERVAL  11 HOUR
| date_added          |
| :------------------ |
| 2021-07-08 06:40:50 |
SELECT CurDATE() + INTERVAL  11 HOUR
| CurDATE() + INTERVAL  11 HOUR |
| :---------------------------- |
| 2021-07-08 11:00:00           |

db<>fiddle here

nbk
  • 45,398
  • 8
  • 30
  • 47