I need to update column pubDate_Date to 2018-02-04 20:39:55 from column pubDate with format Sun, 04 Feb 2018 20:39:55 +0100.
I have tried the following, but without success:
UPDATE `feeds` SET `pubDate_Date` = date('Y-m-d H:i:s', strtotime(`pubDate`))
Hope someone can point me in the right direction
In general, you can use MySQL's STR_TO_DATE() function:
UPDATE feeds SET pubDate_Date = STR_TO_DATE(pubDate, '%a, %d %b %Y %T')
However there's no format code for timezone offsets, which MySQL will therefore ignore from the end of your original string (instead interpreting each string to be in the session's time_zone). Therefore:
If all records are in the same timezone, you could simply do SET time_zone = '+01:00' before the above update command.
Otherwise, do the following instead to adjust each time to the intended timezone:
UPDATE feeds SET pubDate_Date = CONVERT_TZ(
STR_TO_DATE(pubDate, '%a, %d %b %Y %T'),
##session.time_zone,
CONCAT(SUBSTR(pubDate, -5, 3), ':', RIGHT(pubDate, 2)
)
Related
I am trying to use the str_to_Date function to convert a string like
'30/09/2010 3:33:51 p.m.' and '30/09/2010 12:00:00 a.m.'(excluding quotations)
to convert into a date time.
However, I am having trouble adding the last characters being 'p.m.' or 'a.m.' to function
What I have tried is
SET datecolumn = STR_TO_DATE(left(datecolumn(23), '%d/%m%Y %h:%i:%s %_.m.')
<---- returns error code
and
SET datecolumn = STR_TO_DATE(left(datecolumn(18), '%d/%m%Y %h:%i:%s')
<-- But this one excludes the
'a.m.' or 'p.m.'
TIA
For manageAM/PM Use p%
SET datecolumn = STR_TO_DATE(left(datecolumn(23), '%d/%m%Y %h:%i:%s %p')
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
Use REPLACE() to remove the dots from am/pm and apply the correct format:
set datecolumn = str_to_date(
replace(left(datecolumn, 24), '.', ''),
'%d/%m/%Y %r'
)
I use the function LEFT() because it exists in your code, although I don't know if it is really needed.
See the demo.
Results:
| date |
| ------------------- |
| 2010-09-30 15:33:51 |
| 2010-09-30 00:00:00 |
I wanted to convert my date format From MMMM dd,yyyy to yyyy-MM-dd.
I tried using the following:
SET #dt_to = STR_TO_DATE(dateTo, '%d-%m-%Y');
but returns a NULL value.
How will I convert my date to yyyy-MM-dd format in MySQL?
EDITED:
I am creating a procedure in which the value of dateTo was received in the parameter. It is a date in MMMM dd, yyyy format. E.g. October 10, 2015.
NOTE:
The whole query does not return NULL when I use:
SET #dt_to = dateTo;
To convert the date format first you need to use STR_TO_DATE to convert the input string to a date value
SET #dt_to = STR_TO_DATE(dateTo, '%M %d,%Y');
and then convert that date value to your required format
SET #dt_converted = DATE_FORMAT(dt_to, '%Y-%m-%d');
or all in 1 go
SET #dt_to = DATE_FORMAT(STR_TO_DATE(dateTo, '%M %d,%Y'), '%Y-%m-%d');
If it's returning null then that means the extracted datetime value is illegal. You can try like below. See MySQL Documentation for more information.
SELECT STR_TO_DATE('October 10, 2015','%M %d,%Y');
'19/10/2013 2:41'
I have a huge dataset in the above format that I want to insert into my table. But I guess 'YYYY-MM-DD HH:MM:SS' is the format for datetime.
Any solutions?
You can use php preg_split() function.
<?php
$myDate = '19/10/2013 2:41';
$split = preg_split('/[\/\s]/',$myDate);
$formattedDate = $split[2] . '-' . $split[1] . '-' . $split[0] . ' ' . $split[3];
echo $formattedDate;
?>
Since your input is a string in the form 03.09.13, I'll assume (since today is September 31, 2014) that it's dd-mm-yy. You can convert it to a date using STR_TO_DATE:
STR_TO_DATE(myVal, '%d/%m/%y')
Then you can format it back to a string using DATE_FORMAT:
DATE_FORMAT(STR_TO_DATE(myVal, '%d/%m/%y'), '%Y-%m-%d')
Note that the year is %y (lowercase "y") in STR_TO_DATE and %Y (uppercase "Y") in DATE_FORMAT. The lowercase version is for two-digit years and the uppercase is for four-digit years.
I'm importing data from a csv to a MySQL table. I need to convert the date format from String to Date.
From Starting format to finale format:
Mon Feb 04 00:00:00 UTC 2011 ---> 2011-02-04 00:00:00
I've done it sucessfully:
select str_to_date('Mon Feb 04 00:00:00 UTC 2011', '%a %b %d %k:%i:%s UTC %Y');
Now I'm writing the script to do all the import from the csv, there are 2 columns with the date to be converted, but I'm stuck with a MySQL syntax exception on the set part.
My SQL script:
load data local infile 'movimento.csv' into table movimento
fields terminated by ',' lines terminated by '\n'
(id, anno, creditore, #data_pag, #data_spost, descrizione)
set data_pagamento = str_to_date(#data_pag, '%a %b %d %k:%i:%s UTC %Y')
set data_spostamento = str_to_date(#data_spost, '%a %b %d %k:%i:%s UTC %Y')
show warnings;
I'm stuck with a syntax exception on the set part. The error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set data_spostamento = str_to_date(#data_spost, '%a %b %d %k:%i:%s UTC %Y')
show' at line 5
>
What's the right syntax?
The syntax is incorrect. Try this one -
LOAD DATA LOCAL INFILE 'movimento.csv' INTO TABLE movimento
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
(ID, anno, creditore, #data_pag, #data_spost, descrizione)
SET
data_pagamento = STR_TO_DATE(#data_pag, '%a %b %d %k:%i:%s UTC %Y'),
data_spostamento = STR_TO_DATE(#data_spost, '%a %b %d %k:%i:%s UTC %Y')
Hope we're having a good day and all set for Christmas.
Got a quick question. I'm converting a MySQL function into SQL Server, I've got most of the function converted except for one part which has the following:
#description = CONCAT(#description, date_format(#INLastUpdated, '%H:%i %d %b %Y'))
What I'm trying to do is to basically recreate the date_format function to format the date in the same way specified, but I'm not sure how to do it. from what I've seen in the MySQL documentation the format selected would give hour:minute day / short month name / year.
Anyone got any ideas?
You can try this:
DECLARE #description VARCHAR(1000) = 'test'
DECLARE #INLastUpdated DATETIME = '2012-12-21 14:32:22'
SET #description = #description + ' '
+ LEFT(CONVERT(VARCHAR(8), #INLastUpdated, 8), 5) + ' '
+ CONVERT(VARCHAR(20), #INLastUpdated, 106)
SELECT #description
But be careful as format 106 depends on local language settings. Read more on MSDN
The equivalent function is CONVERT. But you're basically out of luck. SQL Server does not allow to cherry-pick the date tokens. You need to browse the available full date built-in formats and choose one, or try to compose an output by string concatenation, as in:
CONVERT(VARCHAR, CURRENT_TIMESTAMP, 103) + ' ' + CONVERT(VARCHAR(8), CURRENT_TIMESTAMP, 114)
Which version of SQL Server? In SQL Server 2012 we now have the FORMAT function.
This, in MySQL
date_format(#INLastUpdated, '%H:%i %d %b %Y')
translates into something like this for SQL Server 2012 using the FORMAT function:
DECLARE #d DATETIME = GETDATE();
SELECT FORMAT(#d, 'HH:mm d MMM yyyy', 'en-US') AS 'DateTime Result';
BOL SQL Server 2012 > FORMAT (Transact-SQL)