Get first date smaller than given date - mysql

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

Related

How can view two month comparison data from one column and show as present and previous as per date range

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?

Ordering MYSQL by date then time

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?

Ordering a mysql table by date when the column is declared varchar

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

MySQL sort on year/month/day

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.

Is it possible to group rows by a day stored within a timestamp?

I'm not sure if this is even within the scope of MySQL to be honest or if some php is necessary here to parse the data. But if it is... some kind of stored procedure is likely necessary.
I have a table that stores rows with a timestamp and an amount.
My query is dynamic and will be searching based on a user-provided date range. I would like to retrieve the SUM() of the amounts for each day in a table that are between the date range. including a 0 if there are no entries for a given day
Something to the effect of...
SELECT
CASE
WHEN //there are entries present at a given date
THEN SUM(amount)
ELSE 0
END AS amountTotal,
//somehow select the day
FROM thisTableName T
WHERE T.timeStamp BETWEEN '$start' AND '$end'
GROUP BY //however I select the day
This is a two parter...
is there a way to select a section of a returned column? Like some kind of regex within mysql?
Is there a way to return the 0's for dates with no rows?
select * from thisTableName group by date(created_at);
In your case, it would be more like
SELECT id, count(id) as amountTotal
FROM thisTableName
WHERE timeStamp BETWEEN '$start' AND '$end'
GROUP BY DATE(timeStamp);
Your question is a duplicate so far: link.