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
Related
I have CHAR strings stored in the database field in the format mm/dd/yyyy. Such as
2/26/2022
2/19/2022
2/12/2022
2/5/2022
12/31/2021
12/18/2021
11/27/2021
I need to sort them as shown according to the "date" without changing the declaration.
The post at MySQL date format DD/MM/YYYY select query? suggested using ORDER BY STR_TO_DATE(datestring, '%d/%m/%Y')
My MySQL statement looks like this:
SELECT stringdate
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%m/%d/%y') DESC
However, the result is not sorted properly. Instead of the desired order as shown above, it is showing like this:
12/31/2021
12/18/2021
11/27/2021
2/26/2022
2/19/2022
2/12/2022
2/5/2022
It seems that the year is being ignored. How can I sort this without actually changing the database field declaration?
Thanks in advance.
2/5/2022 is month and day without leading zeros, and four digit year. The format string you have specified is -
%m - Month, numeric (00..12)
%d - Day of the month, numeric (00..31)
%y - Year, numeric (two digits)
SELECT stringdate
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%c/%e/%Y') DESC
%c - Month, numeric (0..12)
%e - Day of the month, numeric (0..31)
%Y - Year, numeric, four digits
Executing the following query shows the difference in the converted dates -
SELECT
stringdate,
STR_TO_DATE(stringdate, '%m/%d/%y'),
STR_TO_DATE(stringdate, '%c/%e/%Y')
FROM mytable
WHERE product = '#myproduct#'
ORDER BY STR_TO_DATE(stringdate, '%c/%e/%Y') DESC
db<>fiddle
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
%y is the two-digit year code. So you are sorting them all as '20'
%Y is the four-digit year code.
See reference for the date format codes here: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
I recommend you use the DATE data type instead of CHAR.
In my sales table have the date column and the date is formatted like this January 9, 2018, 5:06 pm. How can I specifically get all the sales made from the month of January 2018 using MySQL?
assuming you date column is a date data type columns you can use year() and month() function
select * from my_table
where year(my_date_col) = 2018
and month(my_date_col) = 1
Since
the date is formatted like this January 9, 2018, 5:06 pm
and this is not recognizable time format for MySQL I belive the solution is:
SELECT * FROM table WHERE date like 'January%2018%';
I know this is not a "best practice" as well as storing time in a wrong type.
Since the column isn't already in a DATE format, let's assume it has to stay in its current format for the application purposes and add an auxiliary DATE column to select on (because SELECTs will be more optimized this way).
First we add a VIRTUAL column (requires MySQL 5.7+), which is a calculated column that is calculated at runtime but not stored on disk. We don't need to store this column on disk because we're going to index it later. Our VIRTUAL column will convert the application date's format to a MySQL DATE.
Replace str_date with your current date column:
ALTER TABLE `my_table`
ADD `virtual_date` DATETIME AS (STR_TO_DATE(`str_date`, '%M %d, %Y, %l:%i %p')) VIRTUAL AFTER `date`;
Now add the INDEX:
ALTER TABLE my_table ADD INDEX (`virtual_date`);
Now we can perform a properly indexed SELECT:
SELECT *
FROM my_table
WHERE virtual_date >= '2018-01-01'
AND virtual_date <= '2018-01-31'
or
SELECT *
FROM my_table
WHERE virtual_date >= '2018-01-01'
AND virtual_date <= LAST_DAY('2018-01-01')
The MySQL DATE format is YYYY-MM-DD. The latter query is logically simpler; the LAST_DAY() function will give you the last DATE of the month given a DATE or DATETIME value.
The datatype of date column in my table is char(10). So each date is stored as a string like '02/01/2016/'.
How can I get the day of week and month of year from this '02/01/2016' in mysql?
Convert the string to DATE datatype using STR_TO_DATE function, and then use DATE_FORMAT function.
SELECT DATE_FORMAT( STR_TO_DATE( '02/01/2016', '%m/%d/%Y'), '%w') AS dow
, DATE_FORMAT( STR_TO_DATE( '02/01/2016', '%m/%d/%Y'), '%c') AS moy
(The format specifier needs to match the format of the string. This demonstration assumes that the string is in month/day/year format, that this represents February 1st, and not January 2nd.)
If you want to return a string like 'Monday', use '%W' in place of '%w'
MySQL Reference Manual:
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_str-to-date
As #paxdiablo commented, you should seriously consider storing your date information as date, datetime, or timestamp. That being said, if you must live with your current setup, you can work around this by first parsing your text into a date using STR_TO_DATE() and then extracting out a string weekday and month name using DATE_FORMAT(). Something like this should work:
SELECT DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y'), '%W') AS day_of_week,
DATE_FORMAT(STR_TO_DATE(date, '%d/%m/%Y'), '%M') AS month_of_year
FROM yourTable
Demo here:
SQLFiddle
SELECT * FROM table ORDER BY timestamp;
When i'm trying to order the records by its timestamp i'm getting the following order
08/20/2012 02:09:39 PM
08/20/2012 03:19:08 PM
08/20/2012 09:04:24 AM
08/20/2012 09:05:25 AM
How to change the query so that the records are ordered from AM to PM?
The problem is that your string representation of the timestamp is not in canonical format, that is, sorting the string value does not sort in timestamp order.
To get the rows sorted in order, you can convert the character representation of the value into a DATETIME or TIMESTAMP datatype, or at least into a character representation in a canonical format (e.g. 'YYYY-MM-DD hh:mm:ss' with a 24 hour clock).
The STR_TO_DATE function is useful for converting a string representation in a known format into DATETIME:
SELECT * FROM table
ORDER BY STR_TO_DATE(`timestamp`,'%m/%d/%Y %h:%i:%s %p')
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date
You will want to use STR_TO_DATE()
select *
from dates
order by STR_TO_DATE(dt,'%m/%d/%Y %h:%i:%s') desc
See SQL Fiddle with demo
just add the DESC:
SELECT * FROM table ORDER BY timestamp DESC;
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