The year is expected to be picked as current year instead of the 00 year as shown below.
mysql> select str_to_date('Jul 15 12:12:51', '%b %e %T');
+--------------------------------------------+
| str_to_date('Jul 15 12:12:51', '%b %e %T') |
+--------------------------------------------+
| 0000-07-15 12:12:51 |
+--------------------------------------------+
Expected result:
| 2013-07-15 12:12:51 |
Well, that's intended. Quoting the doc:
Unspecified date or time parts have a value of 0, so incompletely
specified values in str produce a result with some or all parts set to
0. [...] “Zero” dates or dates with part values of 0 are permitted unless the SQL mode is set to disallow such values.
It's easy to fix, btw:
select str_to_date(CONCAT(YEAR(NOW()), ' ', 'Jul 15 12:12:51'), '%Y %b %e %T');
-- July, 15 2013 12:12:51+0000
Simple, ugly, but works:
select str_to_date(concat('Jul 15, ', year(now()), ' 12:12:51'), '%b %e, %Y %T');
Related
INSERT INTO covernote (issue_date) VALUES
( STR_TO_DATE ( '13-May-13','%Y-%m-%d') );
Because May is not %m see https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format - %b for abbreviated month or %M for full month name
+--------------------------------------+--------------------------------------+--------------------------------------+
| STR_TO_DATE( '13-May-13','%Y-%m-%d') | STR_TO_DATE( '13-May-13','%Y-%b-%d') | STR_TO_DATE( '13-May-13','%Y-%M-%d') |
+--------------------------------------+--------------------------------------+--------------------------------------+
| NULL | 2013-05-13 | 2013-05-13 |
+--------------------------------------+--------------------------------------+--------------------------------------+
1 row in set, 1 warning (0.106 sec)
It's a pity you picked may as a representative sample..
Per the documentation on date formatting, %m is the code for "Month, numeric (00..12)".
You are looking for %M, which represents "Month name (January..December)"
I have a date data as below:
Monthyear
Jan 2018
Feb 2018
Mar 2018
I want to convert this into this format:
Monthyear
01-01-2018
01-02-2018
01-03-2018
That is, as a date with the first date of the month.
I tried casting the above and it doesnt work.
Kindly help me out.
You can use STR_TO_DATE:
SELECT STR_TO_DATE(CONCAT('1 ', Monthyear), '%e %b %Y')
e.g.
SELECT STR_TO_DATE(CONCAT('1 ', 'Jan 2018'), '%e %b %Y')
Output:
2018-01-01
The reason for CONCATing a '1 ' to the string is to avoid issues with SQL NO_ZERO_DATE mode (this is discussed in the manual entry for STR_TO_DATE).
use str_to_date()
select str_to_Date(concat('01',Monthyear),"%d %b %Y")
The dates in my table are formatted like this Wednesday 17th May 2017 and Saturday 27th May 2017 and I want to convert it to something like this 17/05/2017 and 27/05/2017 and ORDER BY MyDate.
I tried doing it many different ways and did a lot of research for hours but no luck.
$sql="SELECT * FROM table
WHERE 1 = CASE WHEN deliver = 'Completed' THEN 0 ELSE order = '$order' END
ORDER BY STR_TO_DATE(MyDate,"d-m-Y") ASC";
$res=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_array($res))
{
echo $row['MyDate'];
}
If someone could please help, I would very much appreciate it.
You need to use a proper format when converting string to datetime values
SELECT *,
-- convert to datetime and then format accordingly
DATE_FORMAT(STR_TO_DATE(mydate, '%W %D %M %Y'), '%m/%d/%Y') formatted_date
FROM table1
-- convert to datetime
ORDER BY STR_TO_DATE(mydate, '%W %D %M %Y');
%W - Weekday name (Sunday..Saturday)
%D - Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%M - Month name (January..December)
%Y - Year, numeric, four digits
Sample output:
+------+-------------------------+----------------+
| id | mydate | formatted_date |
+------+-------------------------+----------------+
| 2 | Wednesday 17th May 2017 | 05/17/2017 |
| 1 | Saturday 27th May 2017 | 05/27/2017 |
+------+-------------------------+----------------+
Here is a dbfidlle demo
I have a column which holds values in the format 'June 2015', 'July 2015', 'March 2014', 'February 2016'.
I need to convert these into the format 06/2015, 07/2015, 03/2014, 02/2016.. but also make them a date so I can sort by this column.
I currently have the following select:
SELECT CONCAT(MONTH(STR_TO_DATE(LEFT(SUBSTRING_INDEX(quotation.quotation_exp_conversion_date, ' ', 1),3), '%b')), '/', SUBSTRING_INDEX(quotation.quotation_exp_conversion_date, ' ', -1)) AS exp_conversion_date
FROM quotation
WHERE quotation.quotation_status = 'LIVE'
ORDER BY exp_conversion_date ASC
There are 2 problems. This query converts the values into the format '6/2015', '7/2015', '3/2014', '2/2016' (without the leading 0 for single digit months), and does not order by correctly as the YEAR is not taken into account - so they are ordered:
'2/2016', '3/2014', '6/2015', '7/2015'
I tried wrapping it all in the DATE() function but that just returned NULL for all values.
Any ideas on how to achieve what I want?
Thanks
STR_TO_DATE works for me with a format of '%M %Y':
mysql> select str_to_date('July 2015', '%M %Y');
+-----------------------------------+
| str_to_date('July 2015', '%M %Y') |
+-----------------------------------+
| 2015-07-00 |
+-----------------------------------+
1 row in set (0.10 sec)
Expand your question if there's some reason you can't do the same.
To turn that into a full query (which I obviously can't test without your tables, so caveat lector):
SELECT DATE_FORMAT(d, '%m/%Y')
FROM
(SELECT str_to_date(quotation_exp_conversion_date, '%M %Y') AS d FROM quotation)
ORDER BY d ASC;
i have a date in this format
May 30 2006 12:00AM
is there a equivalent of strtotime() in mysql that will let me convert this to a mysql date?
I think you are looking for the STR_TO_DATE function. Unfortunately, it is not quite as awesome as PHP's strtotime, so you have to give it a format mask to parse:
mysql> SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
-> '2004-04-31'
For your date, I think the mask would be %M %e %Y %l:%i%p, depending on whether or not you are expecting short/long month names and 0-based days. Based on your example it could be either:
mysql> SELECT STR_TO_DATE('May 30 2006 12:00AM', '%M %e %Y %l:%i%p');
+--------------------------------------------------------+
| STR_TO_DATE('May 30 2006 12:00AM', '%M %e %Y %l:%i%p') |
+--------------------------------------------------------+
| 2006-05-30 00:00:00 |
+--------------------------------------------------------+
1 row in set (0.00 sec)
Check out the full reference table for the mask options.
You can use STR_TO_DATE (http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date).
The format should be something like:
"%b %d %Y %l:%i%p"