I have a date that I am sending from controller to query in this format
start date 02/13/2019 end date 03/13/2019
First it was normal timestamp which I used and these query condition seemed to work
DATE_FORMAT(o.Timestamp, '%m/%d/%Y') >= '02/13/2019'")
DATE_FORMAT(o.Timestamp, '%m/%d/%Y') <= '03/13/2019'")
Now I store timestamp in unix format which is 1550077130
After that my query conditions become and don't work.
FROM_UNIXTIME(o.Timestamp, '%m/%d/%Y') >= '02/13/2019'")
FROM_UNIXTIME(o.Timestamp, '%m/%d/%Y') <= '03/13/2019'")
try converting string to date and compare date
select from my_table o
where FROM_UNIXTIME(o.Timestamp) >= str_to_date('02/13/2019', '%d/%m%Y')
AND FROM_UNIXTIME(o.Timestamp) <= str_to_date('03/13/2019', '%d/%m%Y')
Related
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
I have a query with date range like between '01-01-2019' and '03-02-2019' and it is working while I change day(02) of date range like '01-01-2019' and '02-02-2019' then Query is not working.
I have a query with date range from '01-01-2019' and To : '03-02-2019' like
SELECT * FROM `customer_info` WHERE `rec_date` between '01-01-2019' and '03-02-2019'
so it is working but when I change the day of date range like from '01-01-2019' and To : '02-02-2019' and query is
SELECT * FROM `customer_info` WHERE `rec_date` between '01-01-2019' and '02-02-2019'
But query is not working.
I want from this query that it should fetch records after changing day of date range of TO : '02-02-2019'.Please tell me solution why it is not fetching records.
With Only Date:
MySQL date format is this : YYYY-MM-DD. In a simple way it can be queried as
select * from customer_info where rec_date between '2019-01-01' and '2019-02-02'
OR
select * from customer_info where rec_date between >= '2019-01-01' and '2019-02-02'
With Date & Time:
Just use the format which MySQL handles dates YYYY-MM-DD HH:MM:SS and have the query as
select * from customer_info where rec_date >= '2019-01-01' and rec_date <= '2019-02-02'
Especially since MySQL now supports sub-second time precision. Using 23:59:59 is one full second short of the end of a day. where rec_date >= '2019-01-01' and rec_date <= '2019-02-02' is the most precise way to define a date range and will work for any level of time precision from date to sub-second units.
OR
Simple as that.
select * from customer_info where rec_date between '2019-01-01 00:00:00' and '2019-02-02 23:59:00'
That rec_date is probably a VARCHAR or CHAR type instead of a DATE type.
Then it should be transformed to a DATE type before getting a range from that.
Otherwise the BETWEEN will compare the strings alphabetically.
And when you then query a date range, use the 'YYYY-MM-DD' format for date constants.
That won't lead to a confusion with the months and days.
(Is '01-04-2019' the 1st of April, or the 4th of January?)
SELECT *
FROM `customer_info`
WHERE STR_TO_DATE(rec_date,'%m-%d-%Y') BETWEEN '2019-01-01' and '2019-03-02'
I have simple problem with SQL query.. I would like to search fields, where dates begin and ends in specific date and time.
Field is defined as CHAR and have structure: DD.MM.YYYY hh:mm:ss and I cannot change it.
I tried to declare some variables and search by this, also tried by Converting.
This is what I tried and didn't work:
SELECT date FROM table WHERE date BETWEEN '1.01.2017 00:00:00' AND '1.02.2017 23:59:59'
SELECT date FROM table WHERE date >= '1.01.2017 00:00:00' AND date <= '1.02.2017 00:00:00'
SELECT date FROM table WHERE date >= Convert(DATETIME, '1.01.2017', 104) AND date <= Convert(DATETIME, '1.02.2017', 104)
Always after this query I get all dates, not this what I asked.
Your field is not in a format that can be compared lexicographically as strings, so you need to convert it to a date. But it's not in the format that MySQL can parse by default (it expects dates in the format YYYY-MM-DD). So use STR_TO_DATE() to parse it with a format string.
SELECT date
FROM table
WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN '2017-01-01 00:00' AND '2017-01-02 23:59:59'
I solved my problem. Problem was with that HOW this fields are storage in DB.
I thought that they are storage like: DD.MM.YYYY hh:mm:ss, but it was only structure. In DB they are storage like: YYYYMMDDhhmmss and after changes in WHERE query line it works.
SELECT date FROM table WHERE date >= '20170101000000' AND date <= '20170101235959'
SELECT date FROM table WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN '2017-01-01 00:00:00' AND '2017-02-01 23:59:59'
OR
You can use STR_TO_DATE() function.
SELECT date FROM table WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN STR_TO_DATE('1.01.2017 00:00:00','%d.%m.%Y %H:%i:%s') AND STR_TO_DATE('1.02.2017 23:59:59','%d.%m.%Y %H:%i:%s')
You can try above query.
I am trying to select everything from my table with today's date. But I found that that date column is in unix time stamp. So, how do I select everything with today's date? or for example only yesterday's ? If it was normal date instead of unix time it would be easy, but... here is what I have put for my query so far..
$ann_renewal_query = "SELECT * FROM annual_renewal WHERE due_date '%$today%' order BY due_date ASC";
You just need to convert the values to the unix timestamp version. The best way is to convert the current time to Unix timestamp:
where due_date >= UNIX_TIMESTAMP(curdate()) and
due_date < UNIX_TIMESTAMP(date_add(curdate(), interval 1 day)
This version of the query allows it to take advantage of an index on due_date.
I have the following query:
SELECT * FROM incomings WHERE date >= '2011-04-01%' AND date <= '2011-04-29%'
And it shows results from 01-04 to 28-04. This may be a weird question but, it I think it should show results from 29-04 too, right?
What's wrong?
Your syntax is odd. That query would normally be written:
SELECT * FROM incomings WHERE date >= '2011-04-01' AND date <= '2011-04-29'
I think from the way that you're trying to query the data that your date column is actually a DATETIME or TIMESTAMP column. If that's the case then '2011-04-29%' will be being cast to '2011-04-29 00:00:00'
I would recommend you use this SQL instead:
SELECT * FROM incomings WHERE date >= '2011-04-01' AND date < '2011-04-30'
What is the purpose of the "%" here (besides making the date invalid) ?
If "date" is of type DATETIME, then :
'2011-04-29 00:00:00' is <= to '2011-04-29'
'2011-04-29 00:00:01' is not <= to '2011-04-29'
You don't need the leading %, the date without hours is interpreted as midnight (or the very start) of given date.