How to convert dates in "24-Jun-2013" format to MySQL? - 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

Related

Convert a value(string) to timestamp (Input string has unwanted characters) - SQL

I'm trying to convert a value in my table to a timestamp but the value in the table is as below
There's a character 'T' after the date in the string.
GMT_TIME
20210608T111722.837 GMT
1st Goal:
Achieve the result in the below format
Output :
GMT_TIME
2021/06/08 11:17:22:837
2nd Goal: Trying to convert it into EST time
Output
2021/06/08 07:17:22:837
'''
select DATE( DATE_SUB( '1st goal output') , INTERVAL 4 HOUR ) ) from table1
'''
Please give me your valuable input on this.
Thank you
SELECT CAST(SUBSTRING_INDEX(REPLACE('20210608T111722.837 GMT', 'T', ''), ' ', 1) AS DATETIME(3))
Output: 2021-06-08 11:17:22.837.
For to convert from GMT to EST use CONVERT_TZ() function. Remember - you must import timezones into your server system database for to use mnemonic TZ names (not needed if you use time-formatted offsets).
If you want to substract 4 hours unconditionally then
SELECT SUBSTRING_INDEX(REPLACE('20210608T111722.837 GMT', 'T', ''), ' ', 1) - INTERVAL 4 HOUR
In this case the output datatype will be DATETIME(6), if you need strictly DATETIME(3) then use explicit CAST() additionally.

Using str_to_date and INSERT-SELECT with data in mySQL

I am inserting new data from a table, X, from Y. They are identical, except there is a column in X in which the date is formatted according to Str_to_date and an example of a date from Y is:
Mon May 11 03:17:40 UTC 2009. I am struggling with why I am getting an error in my code. My code is as follows:
INSERT into X (str_to_date(REPLACE(date, 'UTC', ''), '%a %M %d %T %Y')) SELECT date FROM Y
When I use an online editor, it works when I replace date with "Mon May 11 03:17:40 UTC 2009" but I am confused as to why it doesn't work when taking from the other columns. Any help is appreciated. Thanks.
The call to str_to_date() needs to be in the SELECT list, not before it.
INSERT INTO X (date)
SELECT str_to_date(REPLACE(date, 'UTC', ''), '%a %M %d %T %Y')
FROM Y
You have to put the convertion and replacing into the select statement
INSERT into X SELECT str_to_date(REPLACE(date, 'UTC', ''), '%a %M %d %T %Y') FROM Y

Converting date and time TEXT fields to one INT unix timestamp field in SQL

I currently have a mySQL database with two TEXT fields: Date and Time. These are in the format 'dd/mm/yyyy' and '0:00' respectively, for example '11/08/2020' and 19:12. I want to create a third field called Timestamp (of type INT) and convert the two original fields into this timestamp field and remove the date/time text fields.
I have done a bit of research in regards to using UNIX_TIMESTAMP() and STR_TO_DATE() but I can't seem to get it to work, the format seems to be wrong for it.
How can I achieve this in SQL and convert two string fields which represent date and time into a third field to replace them both which just stores the unix timestamp?
This is my best attempt so far..
SELECT UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(`InfractionDate`, " ", `InfractionTime`), '%d %M %Y %h:%i%p')) FROM `playerinfractions`
The table is called playerinfractions and the date/time are stored in the TEXT fields InfractionDateand InfractionTime.
Many thanks in advance!
The format pattern that you use in the function STR_TO_DATE()is wrong.
Try this:
SELECT
UNIX_TIMESTAMP(
STR_TO_DATE(
CONCAT(`InfractionDate`, ' ', `InfractionTime`),
'%d/%m/%Y %H:%i')
)
FROM `playerinfractions`
You need to tell STR_TO_DATE the correct format of your date and time, which you suggested was '%d/%m/%Y %H:%i'
SELECT UNIX_TIMESTAMP(
STR_TO_DATE(
CONCAT('10/08/2020', ' ', '12:20'), '%d/%m/%Y %H:%i' )
)
)
FROM `playerinfractions`
So using your columns
SELECT UNIX_TIMESTAMP(
STR_TO_DATE(
CONCAT(`InfractionDate`, ' ', `InfractionTime`), '%d/%m/%Y %H:%i' )
)
)
FROM `playerinfractions`
I tried passing a string to the same function in below format and it worked. Also you can share your format to check it further.
select UNIX_TIMESTAMP(STR_TO_DATE(CONCAT('8/12/2020',' ', '12:01:49' ),'%m/%d/%Y %h:%i:%s'))

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.

Convert string field 'Wednesday, January 30th, 2013, 1:02 PM' to date in mySQL and replace an adjascent field

I have a table TableA with two fields, strField and dateField. The string field strField has text dates like Wednesday, January 30th, 2013, 1:02 PM in it, but I need to convert that to a date format and replace it in the adjacent field called dateField. Is there a simple way of doing this or do I have to do all the stripping ?
The last step is easy, I think. When I strip everything and get the string to look like this 2013-02-15 11:45:21.0 it will be just a simple
STR_TO_DATE('2013-02-15 11:45:21.0', '%m/%d/%Y')
Perhaps there is a function which can strip the data easily and reformat the original text string.
SELECT
DATE_FORMAT(
STR_TO_DATE( 'Wednesday, January 30th, 2013, 1:02 PM', '%W, %M %D, %Y, %I:%i %p' ), '%Y-%m-%d %T');
sqlfiddle link.
For other format conversions, check the DATE_FORMAT() at MySQL docs.
For your UPDATE this would do:
UPDATE `tableA`
SET `dateField` = DATE_FORMAT(
STR_TO_DATE(`strField`, '%W, %M %D, %Y, %I:%i %p'),
'%Y-%m-%d');