MySQL Order By Query for varchar saved date - mysql

I try to order the results of a query by the date which is in the format yyyy/mm/dd and I am using this query to no avail.
SELECT * FROM table ORDER BY STR_TO_DATE(date, '%y/%m/%d')
I can't change the type of field that the date is stored in so I am hoping I can order the date post data entry.
Any help and advice appreciated.

If the date is in that format, you don't need to convert it to a date, surely? That format would sort alphabetically, assuming it is 0 padded... (i.e. July is 3 July 2012 is 2012/07/03...)
So you can just go:
select * from table order by date
What type of field is your date field: are you sure it is a varchar?
Assuming it is a varchar, you can work out what is going wrong by going:
select str_to_date(date, '%y/%m/%d') from table
You (should) get all NULL's, because the %y is wrong. Try:
select str_to_date(date, '%Y/%m/%d') from table
and it should work. But as noted, you don't have to convert to sort.

Try with capital Y:
SELECT * FROM table ORDER BY STR_TO_DATE(date, '%Y/%m/%d')
Lowercase Y is for yy, uppercase for yyyy.

SELECT * FROM table ORDER BY date

Related

Sort by varchar date entry in SQL

I want to sort my table by DATE which is a varchar holding date information like this:
January 06, 2023 // format is Month Day, Year
I want to sort by date but it doesn't work since this is not datetime. I've tried casting DATE but it doesn't seem to work. I tried like this:
SELECT * FROM receipt WHERE ID=? ORDER BY CAST(DATE AS datetime) DESC
I also tried convert but it did not work either, I must be using the syntax wrong.
How can I sort by date if my DATE entry is of type varchar.
In MySQL you can use str_to_date with the appropriate date format to convert a varchar to a date:
SELECT * FROM receipt WHERE ID=? ORDER BY STR_TO_DATE(date, '%M %d, %Y') DESC

MySQL DATE_FORMAT "%d" issue

I'm sure there's a logic to this, but I do not understand it
date_format(date, '%m.%d.%Y') // Output: 03.05.2021
date_format(date, '%D.%m.%Y') // Output: 5th.03.2021
date_format(date, '%e.%m.%Y') // Output: 5.03.2021
But
date_format(date, '%d.%m.%Y') // Output: 28.02.2021
There seems to be an issue when I start the date_format with "%d", but I need my output to be 05.03.2021 (which is 5th March 2021), but I'm not able to achieve that. Can someone please explain this logic to me and help me?
Updated Explanation: In my database I have data from 5th March 2021 to 17th February 2021, so when I SELECT from the table using date_format(date_column, '%d.%m.%Y'), and order by date_column desc, the table begins from 28.02.2021 instead of 05.03.2021
You need to convert the date which is stored as string to a valid mysql date ,yyyy-mm-dd , in the order by for example
SELECT *
from table
order by str_to_date(date_column,'%d.%m.%Y') desc
You could be clearer on how your data is stored by providing samples
Your third example uses %e. Changing that to %d will give you the format you want.
If the value is stored in the database as a DATE, DATETIME or TIMESTAMP, then ordering by that value will give you what you need:
SELECT DATE_FORMAT(date_column, '%d.%m.%Y') AS formatted_date FROM table ORDER BY date_column DESC
N.B. Ordering by formatted_date won't give the order you want, because it's just a text string rather than a DATE or similar.
If the value is not stored in the database as a DATE or similar, then the suggestion from P.Salmon is what you want.

SQL between date with dd.mm.yyyy only matches on day

I want to use a Datepicker to select specific entries in my Database.
The Problem i face is that the entries i get as a reposnse from my query are only sorted for the day part of the date.
I use the date format (varchar field in database):
*dd.mm.yyyy hh:mm ($date = date("Y-m-d") . ' ' . date("H:i");)*
For example:
01.04.2016
10.04.2016
11.04.2016
01.04.2017
10.04.2017
11.04.2017
The Query to get the entries:
SELECT *
FROM order_date
WHERE date >= '10.04.2017' AND date <= '10.04.2017'
Now i expected to get only the to matching entries but
the result of the Query are all four table entries which start with day 10 or 11. Even two of them are in the year 2016.
As i saw in other posts the between XX and YY work if the date is set with yyyy-mm-dd or yyyymmdd but the problem is that i got a database with around 20k entries on which i need a datepicker so i would prefer to use the givin format of the date without rewriting all entries.
Is there a possibilty to get a datepicker between with dd.mm.yyyy format aswell?
Any help highly appriciated
Try something like this,
STR_TO_DATE('10.04.2017', '%d.%m.%Y')
SELECT *
FROM order_date
WHERE date >= STR_TO_DATE('10.04.2017', '%d.%m.%Y') AND date <= STR_TO_DATE('10.04.2017', '%d.%m.%Y')
You can typecast the column as DATE, and use the ORDER BY clause.
Use STR_TO_DATE function may help.
STR_TO_DATE('10.04.2017','%d.%m.%Y')
How about something around this idea ?
SELECT *
FROM Mytable WHERE date
BETWEEN CONVERT(datetime,'10.04.2017',104 )
AND CONVERT(datetime,'10.04.2017',104 )

SQL get date from timestamp

I have a database where all my data have a unix timestamp as a integer, the integer is the amount of seconds since 1.jan 1970.(like what Time.now.to_i returns in ruby http://www.unixtimestamp.com).
Is there any way i can get the date from 1447277423 in SQL? I need to group the rows by date.
I want this to be done in a view, and not use another script to do it.
Is this possible?
Convert the unix timestamp to date only (since you don't want hours/seconds), then group by it.
SELECT * FROM table
GROUP BY FROM_UNIXTIME(date_timestamp, '%Y %m %d');
Or, if you don't want to actually group them and just want them outputted all in order, ORDER BY instead.
SELECT * FROM table
ORDER BY date_timestamp
/* when ordering, it doesn't matter so much if its the whole timestamp or not since the date comes first in the timestamp */
To go one further, you can SELECT the formatted date as well
SELECT *, FROM_UNIXTIME(date_timestamp, '%Y %m %d') as date_Ymd FROM table
ORDER BY date_timestamp;
FROM_UNIXTIME docs
Just use FROM_UNIXTIME():
group by DATE(FROM_UNIXTIME(your_unix_timestamp_field))
That is the nice part of unix timestamps, you can just treat it as an integer.
SELECT something FROM table WHERE date >= 1447277423
Hope this helps

mysql order by closest date/hour before or after

I have a mysql table that has a column that stores dates but isn't in the date format, it's a varchar.
The column is called data_hora and have dates in the dd/mm/yy format, example: 06/09/2012 15:00, so I had to convert to date format in mysql query.
And I need to get the closest date and hour before or after the current time, I came up with the following code, but for some reason it seems to get only closest date but not hour, weird?!?!
SELECT str_to_date(data_hora, '%d/%m/%Y %H:%i') AS data_hora
FROM requisicoes
ORDER BY abs(DATE_FORMAT(NOW(),'%d/%m/%Y %H:%i') - data_hora) LIMIT 1
Help :(
try this:
Your ORDER BY Clause has to be changed
SELECT str_to_date(data_hora, '%d/%m/%Y %H:%i') AS data_hora
FROM requisicoes
ORDER BY abs(TIMEDIFF( NOW() , str_to_date(data_hora, '%d/%m/%Y %H:%i'))) LIMIT 1
You shouldn't format NOW() to a string: it is already a TIMESTAMP value; instead, take the (absolute) difference between the present UNIX_TIMESTAMP() and that of the data_hora alias for your selected STR_TO_DATE() column:
ORDER BY ABS(UNIX_TIMESTAMP() - UNIX_TIMESTAMP(data_hora))
If at all possible, I would advise altering your schema so that your data_hora column is stored as a TIMESTAMP: it will greatly improve the performance of queries of this sort.