Sort on string as date on MySql - mysql

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.

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 Filter Records Set Using DD-MM-YYYY Date Format

I'm a bit confused on how to filter records by date formats.
I have date column of date(yyyy-mm-dd) data type in date table.
Ex:
date
-----
2017-01-29
2017-01-30
I'm want to change the format (dd-mm-yyyy). I'm using this code
SELECT
DATE_FORMAT(`date`, '%d-%m-%Y') as date
FROM
dim_date;
date
-----
29-01-2017
30-01-2017
I want to filter the records with the(dd-mm-yyyy) format. So I tried this code.
SELECT
DATE_FORMAT(`date`, '%d-%m-%Y') date
FROM
dim_date
WHERE DATE_FORMAT(`date`, '%d-%m-%Y') BETWEEN '20-04-2015' AND '06-09-2017';
Results
Nothing
But if I try to filter with the original format (yyyy-mm-dd) It Works.
SELECT DATE_FORMAT(`date`, '%d-%m-%Y') date
FROM dim_date
WHERE DATE_FORMAT(`date`, '%Y-%m-%d') BETWEEN '2015-04-20' AND '2017-01-07';
Why is this weird behavior in Mysql? Am I missing something here?
I also tried with this format DATE_FORMAT(date, '%d-%c-%Y') , DATE_FORMAT(date, '%d-%l-%Y') & DATE_FORMAT(date, '%e-%l-%Y')
No happy face, Please let me known.
Thanks
Max
with your query you convert the date in string and then you are comparing values using between in wrong order ('20' > '06' ) so don't work.
Between require first the min value and second the max value.
If you are working with date you sould convert the string date
so the where between work correcly and avoid the string behavior is filter
SELECT date
FROM dim_date
WHERE date BETWEEN str_to_dat('20-04-2015', '%d-%m-%Y')
AND str_to_date('06-09-2017', '%d-%,-%Y');

Convert varchar in MySql to a given date format

I have a column named 'date' which has type varchar (for reasons) but stores dates in the format- (d-m-Y H:i:s)
Now I have to make a BETWEEN query to find records between two dates. But because the datatype is varchar, I first have to convert the column to date-type and then compare. So I've tried this-
SELECT
mobile,
STR_TO_DATE(date,'%Y-%m-%d')
FROM register
WHERE STR_TO_DATE(date,'%Y-%m-%d')
BETWEEN STR_TO_DATE('2017-05-01','%Y-%m-%d') AND
STR_TO_DATE('2017-05-31','%Y-%m-%d')
This query converts the 'date' column to Y-m-d format correctly. But, the two dates given to compare are taken as strings ("2017-05-01" and "2017-05-31") and the result returns records of the date 2017-05-20 only.
What the query should be so that it compares the column and given dates in date-type?
STR_TO_DATE('DateString', '%e %m %Y %H:%i:%s')
OR
STR_TO_DATE('DateString', '%d %m %Y %H:%i:%s')
Match up the format string to how you have it stored:
SELECT
mobile,
STR_TO_DATE(date,'%Y-%m-%d')
FROM register
WHERE STR_TO_DATE(date,'%d-%m-%Y')
BETWEEN STR_TO_DATE('2017-05-01','%Y-%m-%d') AND
STR_TO_DATE('2017-05-31','%Y-%m-%d')
This query gives what you want
SELECT
user_id,
STR_TO_DATE(date,'%d-%m-%Y')
FROM register
WHERE STR_TO_DATE(date,'%d-%m-%Y')
BETWEEN STR_TO_DATE('05-01-2015','%d-%m-%Y') AND
STR_TO_DATE('31-05-2017','%d-%m-%Y')

MYSQL: how can I get the day of week, month of year from date whose datatype is char(10)

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

How to convert and use varchar data type as a datetime in mysql

Hello all,
This is the format of my my-sql data type "rdate".
Apr 1 2011 01:13:00:000PM
I want to use the order by rdate and i can't make it right order as the data type of rdate is varchar, So i want to convert it to date time , But no success.
I am trying to use date_format(str_to_date(rdate, '%m/%d/%Y'), '%Y%m');
Thanks
Mypixel
Try doing:
ORDER BY str_to_date(rdate,'%M %d %Y %h:%i:%s')
From the docs:
Your Date is in the Following format:
%M Month name (January..December)
%d Day of the month, numeric (00..31)
%Y Year, numeric, four digits
...
You have to tell str_to_date the format that your string is in. This means the way the specific parts of the date are displayed, spaces, etc.
sqlfiddle demo
In your str_to_date function call, you need to specify what the format IS, not what you want it to be. Try this:
str_to_date(rdate, '%M %d %Y %h:%i:%s'));
UPDATE table SET rdate=str_to_date(rdate,'%M %d %Y %h:%i:%s')
Just convert your column for good to datetime.