Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare current time with a time variable in MySQL

I need to write a query in which I should compare current time with a variable which contains some time.

SQL Query:

select time >= curtime() from appointment;

Here time is the variable which has some time.

This is the error I'm getting:

Illegal mix of collations (utf8_general_ci,IMPLICIT) and (latin1_swedish_ci,NUMERIC) for operation '>='; nested exception is java.sql.SQLException: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (latin1_swedish_ci,NUMERIC) for operation '>='

I've already tried using time(now()) but it doesn't work.

like image 231
coder7777 Avatar asked Oct 17 '25 07:10

coder7777


1 Answers

Is appointment.time a DATETIME or similar data type, or is it a string?

It's probably best for your system's integrity if it's a DATETIME or TIMESTAMP, not a string.

At any rate try this

select CAST(time AS DATETIME) >= CURTIME()

The way you've written your expression probably relies on some automatic type coercion, and it's better to make it explicit.

like image 186
O. Jones Avatar answered Oct 19 '25 22:10

O. Jones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!