I want to display the records using two different date. I tried using the Between
select * from billing where select_client = '2' and order_date BETWEEN '01/06/2018' and '30/06/2018' order by id ASC
It returns JULY month records also. I tried used >= and <=. That query also returns same record.
select * from billing where select_client = '2' and order_date >= '01/06/2018' and order_date <= '30/06/2018' order by id ASC
Kindly help me out to get only the records between the two dates. Thanks in advance
You have to convert the strings to a date to compare it:
select * from billing where select_client = '2' and STR_TO_DATE(order_date, '%d/%m/%Y') BETWEEN STR_TO_DATE('01/06/2018','%d/%m/%Y') and STR_TO_DATE('30/06/2018','%d/%m/%Y') order by id ASC
wrong date format. if order_date is mysql DATE then format should be 2018-06-01
Related
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.
This is my SQL to fetch orders of current week:
SELECT * FROM orders
WHERE YEARWEEK(order_date, 1) = YEARWEEK(CURDATE(), 1)
ORDER BY order_date DESC;
But the problem is that it also selects records of future dates in the current week. How to stop that?
Just add a condition on the current date as well:
SELECT o.*
FROM orders o
WHERE YEARWEEK(order_date, 1) = YEARWEEK(CURDATE(), 1) AND
order_date <= CURDATE()
ORDER BY order_date DESC;
Be aware: if you use the order_date column in a function to build the where clause, this will result in a query which does not use an index on order_date and so should be avoided if your table can be big.
If you calculate the first date of the current week you can build your where clause just using order_date and it will use an index.
Maybe this can help to inspire you:
WHERE order_date BETWEEN DATEADD(DAY, -WEEKDAY(CURDATE())), CURDATE()) AND CURDATE()
I haven't tested this but adapted the function names to MySQL using the documentation. The idea is to calculate the first date of the current week so you know the range to filter.
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;
Im currently trying to run a SQL query to export data between a certain date, but it runs the query fine, just not the date selection and i can't figure out what's wrong.
SELECT
title AS Order_No,
FROM_UNIXTIME(entry_date, '%d-%m-%Y') AS Date,
status AS Status,
field_id_59 AS Transaction_ID,
field_id_32 AS Customer_Name,
field_id_26 AS Sub_Total,
field_id_28 AS VAT,
field_id_31 AS Discount,
field_id_27 AS Shipping_Cost,
(field_id_26+field_id_28+field_id_27-field_id_31) AS Total
FROM
exp_channel_data AS d NATURAL JOIN
exp_channel_titles AS t
WHERE
t.channel_id = 5 AND FROM_UNIXTIME(entry_date, '%d-%m-%Y') BETWEEN '01-05-2012' AND '31-05-2012' AND status = 'Shipped'
ORDER BY
entry_date DESC
As explained in the manual, date literals should be in YYYY-MM-DD format. Also, bearing in mind the point made by #ypercube in his answer, you want:
WHERE t.channel_id = 5
AND entry_date >= UNIX_TIMESTAMP('2012-05-01')
AND entry_date < UNIX_TIMESTAMP('2012-06-01')
AND status = 'Shipped'
Besides the date format there is another issue. To effectively use any index on entry_date, you should not apply functions to that column when you use it conditions in WHERE, GROUP BY or HAVING clauses (you can use the formatting in SELECT list, if you need a different than the default format to be shown). An effective way to write that part of the query would be:
( entry_date >= '2012-05-01'
AND entry_date < '2012-06-01'
)
It works with DATE, DATETIME and TIMESTAMP columns.
I have a sql-query where I ask for some dates.
"SELECT DISTINCT date FROM table WHERE condition ORDER BY date");
The output is:
2007-04-08
2008-04-12
2008-09-27
2009-12-06
2010-01-31
2011-02-27
2011-04-15
Now I'm wondering whether it is possible to get the next date from the output above.
Examples:
1. Today is 2008-12-12. The next date would be 2009-12-06.
2. Today is today/current_date (2011-02-22). The next date would be 2011-02-27.
Have you got an idea how to specify the query?
Thank you.
SELECT DISTINCT date FROM table WHERE dateColumn > CURDATE()
Order by dateColumn desc
If you want only the NEXT date, then you can use LIMIT
SELECT DISTINCT date FROM table WHERE dateColumn > CURDATE()
Order by dateColumn desc
LIMIT 1