MySQL Query to check a DateTime column - mysql

I have a rails 3 application that is using a mysql db. Part of the functionality is that I am trying to query a table in my DB for all records where:
updated_at + 1.year <= DateTime.now
But I am having issues writing the query that would give me the expected results. Any ideas how I can go about doing this?

Try date function:
updated_at + interval 1 year <= now()

I would go with
where('updated_at <= ?', 1.year.ago)`
because it is easier to understand.

Related

How to compare date in mysql with index?

my friends,
Currently, I was stuck at the query string to compare the date in MySQL with indexing.
created_at type in table is TimeSTAMP and the value is time string. index_name = created_ato on the same table.
If I tried with created_at = '2020-12-21' then it can use the index but the value is empty cause the created_at is storing at the datetime value.
If I tried with date(created_at) = '2020-12-21' then it can return the data but the index can not apply, so the time to response so slow and got timeout.
Can anyone know about it and give me some advice on how to it fix it?
Many thanks?
What about using:
created_at BETWEEN '2020-12-21 00:00:00' AND '2020-12-21 23:59:59.999'
or perhaps:
created_at >= '2020-12-21' AND created_at < '2020-12-21' + INTERVAL 1 DAY

A Relative time query which will work for both H2 and MySQL

I need to write a filter condition in SQL something of this sort -
select * from table where date > now() - INTERVAL 2 DAY
(Works in MySQL)
But this query fails in H2( Spring Boot Application). Can someone help in formulating the query which will filter date from current time stamp to 2 days before.
Tried different queries - nothing seems to work with H2.
Try DATEADD() function in H2. Alternative to NOW() in H2 is CURRENT_TIMESTAMP
select * from table where date > DATEADD('DAY',-2, CURRENT_TIMESTAMP())
You can try below using DATEADD() function
select * from table where date > DATEADD('DAY',-2, CURRENT_DATE)

How do I remove the date functions from the where clause for the query below?

I am using this query to find some records from my user table in my own timezone. Can someone help out please? This is the query I am using on my MySql DB:
select *
from user
where date(convert_tz(now(), '+00:00', '+05:30')) > date(convert_tz(start_date, '+00:00', '+05:30')) AND
now() < end_date;
Try using datestamp or timestamp as the datatype while you are declaring date. Using this might help your code fetch the date and time format that you are using in your computer.

MySQL DATE_ADD INTERVAL is not working with Hibernate.

My query below, shows hibernate exception:
SELECT DATE_ADD(DATE_FORMAT(MIN(t.time),'%Y-%m-%d'), INTERVAL 6 DAY) FROM Table t;
From what I understand, hibernate doesn't recognize INTERVAL keyword.
Can anyone please help me write an HQL query which gives me the same result as my above query?
(I am trying to get the date post 1 week from the minimum date in my table)
HQL and SQL are two different things. You could use a native SQL query instead of a HQL query. Or you could just execute the following query:
select min(t.time) from SomeEntity e
and add 6 days in Java:
Date minDate = (Date) query.uniqueResult();
minDate = DateUtils.addDays(d, 6); // using apache commons-lang

Use MySQL CURDATE() and CURTIME() to return results up and coming but not passed

Users of my site will be able to create events. They will set a date and a time separately. The event will be saved to a mysql database containing separate date and time columns.
I would like to query the database to return events which are up and coming but not passed. i.e. the event date is today or in the future and if the event date is today check the the time of the event hasn't passed.
I don't know the syntax for this and I cant seem to find it anywhere.
If anyone knows a good way to do this I'd very much appreciate your help.
The simplest way to express this efficiently in SQL and make use of any indexes you have on the date and/or time columns would be a query like this:
SELECT *
FROM your_table
WHERE date_column > current_date()
OR (date_column = current_date() AND time_column > current_time())
Depending on which version of MySQL you are using and how your table is indexed there is a small chance that the optimizer would prefer the query expressed as two SELECTs UNIONed together to avoid the OR:
SELECT *
FROM your_table
WHERE date_column > current_date()
UNION
SELECT *
FROM your_table
WHERE date_column = current_date()
AND time_column > current_time()
select * from table where concat_ws(' ',date_field,time_field) > now()
Why don't you use a datetime field?
in pseudocode
if ((date > today) OR (date == today AND time > now))