Sql between two dates - mysql

I have this query:
SELECT *
FROM some_table
WHERE id != 1 AND
(event_date BETWEEN '20/06/2015' AND '01/07/2015')
ORDER BY the_date
The result is 0.
If I try this query:
SELECT *
FROM events
WHERE id != 1 AND
(event_date BETWEEN '20/06/2015' AND '29/06/2015')
ORDER BY the_date
It works.
What is the problem with the difference between tow month (Juny and July).
Thanks.

Seems like you're comparing these dates lexicographically. Assuming that event_date is a date column, use str_to_date to convert the string literals to dates:
SELECT *
FROM some_table
WHERE id != 1 AND
(event_date BETWEEN STR_TO_DATE('20/06/2015', '%d/%m/%Y') AND
STR_TO_DATE('01/07/2015', '%d/%m/%Y'))
ORDER BY the_date

Write date constants in the ISO standard YYYY-MM-DD format:
SELECT *
FROM some_table
WHERE id != 1 AND
event_date BETWEEN '2015-06-20' AND '2015-07-01'
ORDER BY the_date

MySQL uses the following data types for saving a date:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
Try changing the date format

Related

Format a string to a date in MySQL query

I have a table as shown below, the date column is a string(MMMM-yyyy).
I want to select the latest row for an ID. For ID # 2, the latest date would be August-2020 with the price of 45.40
ID Date Price
2 August-2020 45.40
2 July-2020 42.30
I've tried the format(date, 'MMMM-yyyy) as formatted date and STR_TO_DATE(date, '%MMMM-%yyyy') but I can't get it to convert to a date so I can order the column. I want to keep the date in this format, I just need to order it for my query.
Use str_to_date() to convert the string to a date; a little trick is needed to make the original value a complete date string before conversion, so:
str_to_date(concat(date, '-01'), '%M-%Y-%d')
To filter the table on the latest date per id, you would do:
select t.*
from mytable t
where date = (
select date
from mytable t1
where t1.id = t.id
order by str_to_date(concat(date, '-01'), '%M-%Y-%d') desc
limit 1
)
You need to proper format specifiers for MySQL. I would expect this to work:
select STR_TO_DATE('August-2020', '%M-%Y')
However, it appears that you need:
select str_to_date(concat('01-', 'August-2020'), '%d-%M-%Y')
Here is a db<>fiddle.

Order by date when a format 'dd-MM-YYYY' is given in the select statement is not working as expected

I store the date in a DATETIME field in my database. When I create a select statement I need to show it with format '%d-%m-%Y' but I have problems sorting the data by date.
this is my selec statement:
SELECT
DATE_FORMAT(Date,'%d-%m-%Y') AS Date
FROM Purchase
WHERE Date BETWEEN '2019-02-01' AND '2019-06-30' ORDER BY Date asc;
But the data does not get sorted by date. How can I solve this?
If you use the same name for the column date and formatted date then the order by work for the alias column .. so is order by day, month, year as in your format ('%d-%m-%Y')
then try changing the alias name
SELECT DATE_FORMAT(Date,'%d-%m-%Y') AS My_Date
FROM Purchase
WHERE Date BETWEEN '2019-02-01' AND '2019-06-30'
ORDER BY Date asc;
or use a proper order by format
SELECT DATE_FORMAT(Date,'%d-%m-%Y')Date
FROM Purchase
WHERE Date BETWEEN '2019-02-01' AND '2019-06-30'
ORDER BY DATE_FORMAT(Date,'%Y-%m-%d') asc;
You can solve this by telling SQL to order by the Purchase.Date field instead of the formatted string date value. When you chose to order by "Date" with no table or table alias specified, SQL assumes you want the formatted result aliased as Date, not the "Date" field from your Purchase table.
SELECT DATE_FORMAT(Date,'%d-%m-%Y') AS My_Date
FROM Purchase
WHERE Date BETWEEN '2019-02-01' AND '2019-06-30'
ORDER BY Purchase.Date asc;

How to check if datetime equal tomorrow in MySQL

I want to retrieve mysql table data if the data created_at Datetime column equal to tomorrow date, for example:
SELECT * FROM sales_order where created_at = tomorrow_date;
You can use the following solution, using DATEDIFF and DATE_ADD:
SELECT *
FROM sales_order
WHERE DATEDIFF(created_at, DATE_ADD(CURDATE(), INTERVAL 1 DAY)) = 0;
or a simpler solution only using DATEDIFF:
SELECT *
FROM sales_order
WHERE DATEDIFF(created_at, CURDATE()) = 1
DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation. - from MySQL docs.
SELECT * FROM sales_order where created_at = CURDATE() + 1;

How to convert a mysql datetime column to just date?

Here is an explanation of how my table is set up (in sqlfiddle)
http://sqlfiddle.com/#!2/86a54d/2
Basically, I have two columns. One is a varchar and contains a name. The other is a datetime and contains a certain date with a time.
I want to select rows where the date in the datetime matches today's date. I have tried things like...
select * from alerts_to_send where date_to_send = now()
select * from alerts_to_send where date_to_send = curdate()
I'm not exactly where to go from here... Any ideas would be greatly appreciated :)
Just to re-clarify, I want to select the rows where today's date matches the date part of my datetime type column.
try this
select * from alerts_to_send
where DATE_FORMAT(date_to_send,'%m-%d-%Y') = DATE_FORMAT(now(),'%m-%d-%Y')
fiddle
You can use the date_format function for mysql
select * from alerts_to_send where date_format(date_to_send, "%m/%d/%Y") = date_format(now(), "%m/%d/%Y")
Here is reference to date_format: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
An alternative to date_format function you can convert the datetime to date:
select * from alerts_to_send where cast(date_to_send as date) = curdate()
It could be even shorter:
select * from alerts_to_send where date(date_to_send) = date(now())
But you will have problem with using index on it so better will be:
select * from alerts_to_send where date_to_send >= date(now()) and date_to_send < date(now()) + INTERVAL 1 DAY

Mysql - Group by month with Y-m-d format

If I have column 'date' in format 'Y-m-d', can I use GROUP BY based on date column but group by months?
Use MySQL's DATE_FORMAT() function to group only on the desired parts:
GROUP BY DATE_FORMAT(my_column, '%Y-%m')
For Example: created_at is the column you need
SELECT DATE_FORMAT(`created_at`, '%Y-%m-%d')
FROM `table`
GROUP BY DATE_FORMAT(`created_at`, '%Y-%m-%d')
Note that you will need to select created_at.