Convert March 2015 to 03/2015 and make into date - mysql

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;

Related

making Date Mon in one column equal to yyyy-mm-dd hh:mm:ss

In my table i'm having two fields created_date and created_month.
created_date: DATETIME e.g. 2020-12-17 17:23:56
created_month: VARCHAR e.g. 17-Aug
Now the situation- suppose for a record the created_date is 2020-12-17 17:23:56 and created_month is 17-Aug.
I am trying to achieve that the created_date should become like this 2020-08-17 17:23:56. The month from created_month gets updated in created_date. Not very much particular about time or date (17) but year yes.
If I'm doing this
UPDATE my_table SET created_date = STR_TO_DATE(created_month, '%d %b')
It is giving this 0000-08-12 00:00:00 . the year is 0000
put created_month in created_date without altering the year
UPDATE my_table
SET created_date = STR_TO_DATE(CONCAT(created_month,
'-',
YEAR(created_date),
' ',
SUBSTRING_INDEX(created_date, ' ', -1)),
'%d-%b-%Y %H:%i:%s');
fiddle

How to set Order BY to specific variable structure

Essentially I have a column in my mySQL database that contains time intervals like:
8:00 am - 12:00 pm
The column accepts VARCHAR values.
How can I ORDER BY using this column data in DESC order?
I would like the query to understand am from pm and order in terms of that.
One method is to convert to times:
order by str_to_date(concat('2000-01-01 ', substring_index(str, ' - ', 1)), '%Y-%m-%d %h:%i %p'),
str_to_date(concat('2000-01-01 ', substring_index(str, ' - ', -1)), '%Y-%m-%d %h:%i %p')
The date is irrelevant for the order by, because they are all the same.
Here is a db<>fiddle showing the values produced.

How to convert dates in "24-Jun-2013" format to MySQL?

I am trying to convert a large table that has dates in the format Day of Month - Month Abbreviation - Year in a row separated by dashes, like 01-Jan-2014. The closest question I found is this one where the asker ended up doing a search and replace method instead. I could do that too, but I think that surely there is a better way. My code:
SELECT STR_TO_DATE(datecolumn, '%c/%d/%Y') FROM 'Table'
Returns null with dates in the above format but is fine with other date formats. I also tried DATE_FUNCTION and other suggestions I saw online.
This will do it:
SELECT
DATE_FORMAT(
STR_TO_DATE(
REPLACE(
SUBSTRING_INDEX(datecolumn,'-',-2),
'-',
CONCAT(' ', SUBSTRING_INDEX(datecolumn, '-', 1), ', ')
),
'%M %d, %Y'
),
'%Y-%m-%d'
)
FROM `Table`;
Explanation:
We use SUBSTRING_INDEX(datecolumn,'-',-2) to get the last two pieces, like Jan-2014
We replace the - in Jan-2014 with the first piece (using SUBSTRING_INDEX), i.e., 01, but also use CONCAT to add spaces and ,, so we get Jan 01, 2014
We use STR_TO_DATE and tell it the format is %M %d, %Y
We then put it in %Y-%m-%d format with DATE_FORMAT()
Examples:
07-Jan-2014 -> 2014-01-07
9-May-2001 -> 2001-05-09
13-Dec-1978 -> 1978-12-13
Here's a fiddle

using current year in string to date function

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');

Converting varchar to datetime type [duplicate]

This question already has answers here:
Converting a date in MySQL from string field
(4 answers)
Closed 9 years ago.
I need help with converting varchar type column which is having date time values to DATETIME type and I need only the Time part from it. i.e. I've a column with values like:
Verified_date
04/04/2013 03:17:08 PM
05/04/2013 10:10:13 AM
04/04/2013 03:45:15 PM
I need only the time part, i.e,
Time
03:17:08 PM
10:10:13 AM
03:45:15 PM
You can use this query using DATE_FORMAT:
Select DATE_FORMAT(STR_TO_DATE(('04/04/2013 03:17:08 PM'), '%d/%m/%Y %r '), '%r')
This statement will give you the required formatted date as 03:17:08 PM.
You can view the SQL Fiddle.
Try this query, just add ending formatting -
SELECT TIME(STR_TO_DATE('04/04/2013 03:17:08 PM', '%d/%c/%Y %h:%i:%s %p'));
+----------+
| 15:17:08 |
+----------+
One more simple solution -
SELECT SUBSTRING_INDEX('04/04/2013 03:17:08 PM', ' ', -2);
+-------------+
| 03:17:08 PM |
+-------------+
Try this
select DATE_FORMAT(STR_TO_DATE(start, '%d/%m/%Y %r'), '%r') from your_table;
Refer date-and-time-functions