MySQL: WHERE TimeStamp after today - mysql

I would like to SELECT 5 Items WHERE unix_timestamp is after today.
Example:
unix_timestamp (here not the format):
15.08.2013
18.08.2013
21.08.2013
27.08.2013
30.08.2013
MySQL: SELECT * WHERE unix_timestamp is today or after today and ORDER BY unix_timestamp with LIMIT 0,5
MySQL Result must be items with the unix_timestamp:
18.08.2013
21.08.2013
27.08.2013
30.08.2013

This should actually work (DEMO):
SELECT FROM_UNIXTIME(unix_timestamp, '%m.%d.%Y') as `Date` FROM dates WHERE FROM_UNIXTIME(unix_timestamp, '%m.%d.%Y') >= DATE_FORMAT(NOW(), '%m.%d.%Y') ORDER BY unix_timestamp Limit 5;
I've assumed that because you have named your column unix_timestamp, your date is actually stored in that format (eg. 1376825624).
Note, that I would consider to change the name of unix_timestamp because there is allready a function in MySQL called UNIX_TIMESTAMP().

You can try something like this :
SELECT * FROM FOO
WHERE UNIX_TIMESTAMP >= NOW() ORDER BY UNIX_TIMESTAMP Limit 5;
NOW() Return the current date and time
If the UNIX_TIMESTAMP column contains the data in date format as per the example then instead of NOW() you can use CURRENT_DATE().
i.e.
SELECT * FROM FOO
WHERE UNIX_TIMESTAMP >= CURRENT_DATE() ORDER BY UNIX_TIMESTAMP Limit 5;
CURRENT_DATE() Return the current date
Date and Time Functions

Related

Selecting from table where timestamp between X doesn't return all rows

I have database table with different records and they all have timestamp with them.
When I want to get a certain month (for example April) records is use following query:
SELECT *
FROM `water`
WHERE timestamp >= DATE_FORMAT('2020-04-01', '%Y-%m-%d')
AND timestamp <= DATE_FORMAT('2020-04-30', '%Y-%m-%d')
AND watercar='JV03'
ORDER by timestamp DESC
It will return me records which timestamp is between 01.04.2020-29.04.2020 but it misses the last day of april 30.04.2020 record.
I also tried >= <= and between operators, same issue although the record does exist.
What am I missing?
DB Fiddle: https://www.db-fiddle.com/f/nWFFZmUt7FM17c98DXRRQw/0
Update your query to this:
SELECT *
FROM `water`
WHERE timestamp between DATE_FORMAT('2020-04-01', '%Y-%m-%d 00:00:00') AND DATE_FORMAT('2020-04-30', '%Y-%m-%d 23:59:59') AND watercar='JV03'
ORDER by timestamp DESC
or
SELECT *
FROM `water`
WHERE DATE(timestamp) between DATE_FORMAT('2020-04-01', '%Y-%m-%d') AND DATE_FORMAT('2020-04-30', '%Y-%m-%d') AND watercar='JV03'
ORDER by timestamp DESC
First, there should be no need to use date_format). MySQL should understand dates in the YYYY-MM-DD format.
Second, do not use between with date/time values. Instead, to get everything in April, use:
where timestamp >= date('2020-04-01') and
timestamp < date('2020-05-01')
This formulation works both when the column as a time component and when it does not. So, I recommend it in all situation.
If you want to pass in the end date as a parameter, you can use:
where timestamp >= :start_dt and
timestamp < :end_dt + interval 1 day

Is there a function similar to Oracle's trunc(sysdate) in mysql

In Oracle I can query like:
Select * from Orders where trunc(createdDate) = trunc(sysdate)
and I get all orders created today.
How to do the same in MySql?
So far I can only find like currentdate() = '07-05-2017', I don't want to hard code the date.
Thank you,
Dapid Candra
The date() function returns the date part of any datetime value and curdate() function returns the current date, so no need to truncate its result:
... date(createdDate)=curdate()
I think a bit faster
Select *
FROM Orders
WHERE createdDate >= curdate();
Yes, you can use MySQL's DATE() function (along with NOW() function to get the current date). Here's the documentation.
Extracts the date part of the date or datetime expression expr.
Your query would be:
Select *
FROM Orders
WHERE DATE(createdDate) = DATE(NOW())
Update
As suggested by #Rahul, we can replace DATE(NOW()) with CURDATE() as it returns date without timestamp, e.g.:
Select *
FROM Orders
WHERE DATE(createdDate) = CURDATE()

Query MYSQL with a date in the format of 10-OCT-15

I am trying to query a MYSQL database to return all records with today's date -
SELECT *
FROM credit_application
created_on = '15-OCT-15';
But it's failing because of the 'OCT' part within the query. How can I resolve this please?
Use mysql DATE_FORMAT function
SELECT *
FROM credit_application
DATE_FORMAT(created_on,'%d-%b-%y') = '15-OCT-15';
Use mysql STR_TO_DATE function
SELECT *
FROM credit_application
created_on = STR_TO_DATE('15-OCT-15', '%d-%b-%y');
Of course, this assumes your created_on field is just a DATE. If it's actually a DATETIME or TIMESTAMP, then you'll need to do a range query instead:
SELECT *
FROM credit_application
created_on >= STR_TO_DATE('15-OCT-15', '%d-%b-%y') AND
created_on < DATE_ADD(STR_TO_DATE('15-OCT-15', '%d-%b-%y'), INTERVAL 1 DAY);
The other suggestions of using DATE_FORMAT would require the function to be applied to every row in the table, preventing use of any index you might have. It would be a non-sargable query.

how to search current date in mysql database that store standard date as ('Y-m-d H:i:s')?

I store standard date ( with this format: ('Y-m-d H:i:s') ) in mysql database, now i want to select records that match this standard date with current date, in other word i want to select the rows where standard_date field demonstrate today's date.
use DATE() to strip off time in the datetime column. CURDATE() returns the current date.
SELECT *
FROM tableName
WHERE DATE(standard_date) = CURDATE()
SQLFiddle Demo (DATE() vs without DATE())
Just use:
select * from mytable where date(standard_date) = curdate();
select * from tablename where date = CURDATE()
CURDATE() returns the current date.
If you use DATE type, use CURDATE() function -
SELECT * FROM table WHERE date_field = CURDATE()
If you use DATETIME type, use CURDATE() function and DATE() function to get date part from datetime value -
SELECT * FROM table WHERE DATE(date_time_field) = CURDATE()

how to test date and datetime with mysql

My table is using a datetime (YYYY-MM-DD HH:MM:SS) and i need to display today's entries.
my code is only :
SELECT *
FROM table
WHERE date = '$date'
ORDER BY score DESC
with
$date = date("Y-m-d");
well, as expected it doesnt work :| you guys have a solution here ?
Following from Pascal Martin, you could extract the date part from the date+time field:
SELECT * FROM table WHERE DATE(date) = '2009-12-19'
Source: MySQL - Date and Time Functions
Be aware however, that this query will not use an index on your date+time field, if you will be having one. (Stack Overflow: How does one create an index on the date part of DATETIME field in MySql)
Your date is "2009-12-19" (or something like that, depending on the day), which is interpreted as "2009-12-19 00:00:00".
In your database, you probably don't have any date that's exactly equal to that one, by the second : your dates are like "2009-12-19 12:15:32".
A solution is to compare like this :
select *
from table
where date >= '2009-12-19'
and date < '2009-12-20'
Which will be interpreted as :
select *
from table
where date >= '2009-12-19 00:00:00'
and date < '2009-12-20 00:00:00'
And, if you don't want to do the math to get the date of the following date, you can use the adddate function :
select *
from table
where date >= '2009-12-19'
and date < adddate('2009-12-19', interval 1 day)
So, in your case, something like this should do the trick :
select *
from table
where date >= '$date'
and date < adddate('$date', interval 1 day)
order by score desc
You probably want to format the data when you select it:
SELECT *, DATE_FORMAT(date, '%Y-%m-%d') AS dateformat FROM table
WHERE dateformat = '$date' ORDER BY score DESC
You are comparing datetime and date expression, that is why its not working. Use Date() method to return the date part from datetime and then do the comparison. WHERE DATE(date) = '$date' should do. You might have to use aliases to handle this name collision.