Help?
When using Amazon Seller Central's API, each order returns a date in this format:
2018-09-01 06:40 PM PDT
I'd like to convert it to a MySQL-friendly format like:
2018-09-01 18:40:00
How can I update these values in my staging table with a MySQL query?
Thanks in advance!
You can use STR_TO_DATE to convert the string to a valid MySQL date format.
SELECT STR_TO_DATE(SUBSTRING('2018-09-01 06:40 PM PDT', 1, 19), '%Y-%m-%d %l:%i %p') AS date;
If you want to convert the timezone too, you can use CONVERT_TZ.
SELECT
CONVERT_TZ(
STR_TO_DATE(SUBSTRING('2018-09-01 06:40 PM PDT', 1, 19), '%Y-%m-%d %l:%i %p'),
'PST8PDT', -- Note: PDT is not a valid MySQL timezone
'UTC'
) AS date
Related
I am importing a CSV file I download from a website to display to clients in my PHP App. The date the website spits out in the CSV is not MySQL friendly. I want to convert it in the table immediately after importing the CSV file. The date in the CSV file is this format: February 06 2023 08:26:44
I'm running the following query:
update tblname set `time` = STR_TO_DATE(`time`, '%Y-%m-%d %H:%i:%s') where `time` = STR_TO_DATE(`time`, '%M %d %Y %H:%i:%s');
This is the error I get in phpmyadmin:
MySQL said: Documentation
#1292 - Truncated incorrect datetime value: 'February 06 2023 08:26:44'
Due to the nature of the log, I need the time to be kept as well. Any help much appreciated!
STR_TO_DATE(time, '%M %d %Y %H:%i:%s') parses the formatted date and returns the date that you want to store in the table:
UPDATE tblname
SET `time` = STR_TO_DATE(`time`, '%M %d %Y %H:%i:%s');
The WHERE clause is not needed.
I have a DateTime column, which (because of some reasons) has changed my DateTime values from DateTime to Strings. Now I want to change them back to Date only. How to do this?
e.g my date column has values like: "Sep 2, 2019 8:00:00 PM" (in string format)
select str_to_date('Date','%d-%m-%Y') as Date
from table
This code gives blank cells as a result
Your date's format is: '%b %e, %Y %h:%i:%s %p'.
Read more about the parameters you can use in str_to_date() here.
So do the conversion like this:
select str_to_date(
'Sep 2, 2019 8:00:00 PM',
'%b %e, %Y %h:%i:%s %p'
) as Date
See the demo.
Have you tried using the CONVERT function?
SELECT CONVERT(table.Date, DATE) AS Date FROM table;
I use the below method to convert Unix Time stored in the hltime column of a MySQL DB to a readable format - "YYYY DD MM hh:mm:ss"
For Example:
UTC date and time stored format in MySQL DB: 1500473820
MySQL Function used to convert to readable format:
from_unixtime(hltime,'%Y %D %M %h:%i:%s')
Result: "2017 19th July 07:47:00"
The date and time stored in the hltime column are in GMT. Thus how could I use the from_unixtime() function (or any other function) to convert the values shown into IST (Indian Standard Time).
I have tried using convert_tz(from_unixtime(hltime,'%Y-%D-%M %h:%i:%s'),'+00:00','+06:00'), but I get NULL values.
Date time format used to convert is not correct
Change:
'%Y %D %M %h:%i:%s'
To:
'%Y-%m-%d %h:%i:%s'
And it should be working
Example:
SELECT #ut:= 1500473820 AS ut
, #ts:=FROM_UNIXTIME(#ut,'%Y-%m-%d %h:%i:%s') AS gmt
, CONVERT_TZ(CAST( #ts AS DATETIME ),'+00:00','+05:30') AS ist;
Result:
---------- ------------------- ---------------------
ut gmt ist
---------- ------------------- ---------------------
1500473820 2017-07-19 07:47:00 2017-07-19 13:17:00
How do I convert the following format to unix timestamp?
Apr 15 2012 12:00AM
The format I get from DB seems to have AM at the end.
I've tried using the following but it did not work:
CONVERT(DATETIME, Sales.SalesDate, 103) AS DTSALESDATE,
CONVERT(TIMESTAMP, Sales.SalesDate, 103) AS TSSALESDATE
where Sales.SalesDate value is Apr 15 2012 12:00AM
Here's an example of how to convert DATETIME to UNIX timestamp:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))
Here's an example of how to change date format:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p')),'%m-%d-%Y %h:%i:%p')
Documentation: UNIX_TIMESTAMP, FROM_UNIXTIME
You will certainly have to use both STR_TO_DATE to convert your date to a MySQL standard date format, and UNIX_TIMESTAMP to get the timestamp from it.
Given the format of your date, something like
UNIX_TIMESTAMP(STR_TO_DATE(Sales.SalesDate, '%M %e %Y %h:%i%p'))
Will gives you a valid timestamp. Look the STR_TO_DATE documentation to have more information on the format string.
If you want to create a timestamp as returned by java's Date.getTime() you should multiply by 1000.
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))*1000
Now for a more standard date format use:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('2022-12-14 20:58:00', '%Y-%m-%d %H:%i:%s'))*1000
From http://www.epochconverter.com/
SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())
My bad, SELECT unix_timestamp(time) Time format: YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD. More on using timestamps with MySQL:
http://www.epochconverter.com/programming/mysql-from-unixtime.php
In mysql database I store date column as unix-timestamp format (for example:1264105904). I want to convert this date to datetime like "Oct 11, 2011 6:25 am PDT" in select statement. How can I achieve this?
select date_format(from_unixtime(1264105904),'%b %d, %Y %l:%i %p PDT');
should do what you want