I have a large list of dates of the format dd-mm-yyyy.
So I want to order on: year, then on month and then on day.
Date and month are in the same field and year is an other field.
I have now: ORDER BY table.year ASC, table.date ASC
The result is that the list is order on year and then days.
How to split/strip the dd-mm format and first sort on month before sorting on days?
Same record:
date | year
dd-mm | yyyy
based on your example you can sort the record like this,
ORDER BY STR_TO_DATE(CONCAT(year, '-', date), '%Y-%d-%m') ASC
SQLFiddle Demo
As per my knowledge it's better to use single date type field instead of having seperate two fields for date-month and year as you can easily sort your results.
According to your query date-month can be stripped out using RIGHT(date-monthfield, 2) function. This selects month which is on the right side.
The query would be:
select RIGHT(date-monthfield, 2) from table ORDER BY date-monthfield ASC;
Hope it helps.
Related
SELECT itemcode,itemname,(SUM(good)+SUM(bad)) as prdn,SUM(good) as good,SUM(bad) as bad,concat(round(((SUM(bad)/(SUM(good)+SUM(bad)))*100),0),'%') as PresentRejection,
concat(round(((SUM(bad)/(SUM(good)+SUM(bad)))*100),0),'%') as PreviousRejection
FROM tb_data
WHERE date BETWEEN '2022-07-01' AND '2022-07-14'
GROUP BY itemcode
ORDER BY good DESC;
I want to show PresentRejection and PreviousRejection data beside one another. PreviousRejection date will be '2022-06-01'AND '2022-06-30', Now how can I do this?
I am storing several dates in a MySQL database.
1992-01-03
1990-02-30
1990-01-28
1990-01-13
1990-01-01
(Note: The order of the dates is not the same as the order in my database)
If I referenced the date 1990-01-29 for any arbitrary reason, and I needed to get the first date that was smaller than 1990-01-29, how could I create a query that would do that for me?
Search for all dates that are less than the one you want, sort the result and just grab one row. Something like:
SELECT theDate
FROM yourTable
WHERE theDate < '1990-01-29'
ORDER BY theDate DESC
LIMIT 1
I am trying to order MYSQL by date then time.
SELECT START_DATE FROM Table ORDER BY DATE(START_DATE) DESC, TIME(START_DATE) ASC
so what i am trying to do is to get all entries ordering by latest date then order those entries by earliest time.
Exampe:
2016-11-6 6:45:00
2016-11-6 6:30:00
2016-11-6 6:15:00
2016-11-5 6:30:00
2016-11-4 6:30:00
2016-11-4 6:15:00
i want to have the data ordered like the above example but, i then want to have 2016-11-4 6:15:00 on top of 6:30:00. any way to do this?
I need to sort a table by date (descending), but all columns in the table are varchar, so I need to manipulate the data on the fly for sorting it correctly.
date sales
10/09/2014 100
13/09/2014 250
30/08/2014 200
Is that possible without altering the table? So the result will be like below, newest dates first?
date sales
13/09/2014 250
10/09/2014 100
30/08/2014 200
Like pseudocode
SELECT * FROM table ORDER BY (CONCAT(REGEXP(date, '[0-9]{4}'),
REGEXP(date, '/[0-9]{2}/'), REGEXP(date, '^[0-9]{4}/')) DESC
I think I need to use substring_index somehow, because regexp just returns 1 or 0, not the actual value found.
You need to convert your varchar-stored date objects into DATE objects, then use them to order.
This you can do on the fly like so
ORDER BY STR_TO_DATE(date,'%d/%m/%Y') DESC
But performance is going to be horrible. For best results store your dates in a DATE column in your table.
you can use STR_TO_DATE
SELECT *
FROM Table1
ORDER BY STR_TO_DATE(date, '%d/%m/%Y') desc,
sales desc
How can I filter the dates that it would sort first by today then normal?
I have a column with data type datetime, I wanted my results to be sorted showing today's date first and continue normal sorting.
What about
SELECT
...
FROM
...
ORDER BY IF(DATE(datefield=CURRENT_DATE()),0,1), datefield DESC
Edit
Added the DESC to the ORDER BY after the 3rd comment to the OQ