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
Related
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 want to extract the date as the format 2008-07-06 from this date Sunday 6th July 2008 format.
I have tried as below by using below two functions
select str_to_date('Sunday 6th July 2008','%Y-%m-%d')
select DATE_FORMAT('Sunday 6th July 2008','%Y-%m-%d')
but it will returning null value.
Use STR_TO_DATE:
SELECT STR_TO_DATE('Sunday 6th July 2008', '%W %D %M %Y')
FROM dual;
The default date format in MySQL already is 2008-07-06, so you should not have to make a second call to DATE_FORMAT.
SQLFiddle
here i got a date format and a given date i need to convert the date into the format can anyone help me to do that.
i have tried with couple of mysql functions like date_format etc..
29-AUG-12 06.06.32.000000000 PM,DD-MON-RR HH.MI.SS.FF AM
Thanx in advance..
You can try a solution like this according to format date is coming and in which format you want to convert
SELECT DATE_FORMAT(STR_TO_DATE('14 Aug 2014 07:46:17:000000', '%d %b %Y %T:%f'),'%d-%m-%Y %h:%i %p') AS T
output- 14-08-2014 07:46 AM
I have a time stamp of the form "17:16:28 Sep 13, 2011 PDT" in a MySQL database. The type of the field in the database is VARCHAR. I would like to convert this VARCHAR to a field of type TIMESTAMP in MySQL.
I tried a few solutions suggested elsewhere on this site, such as using a string_to_time function, but these solutions all start from a different type of timestamp.
How do I convert the VARCHAR timestamp mentioned above to a TIMESTAMP recognised by MySQL, in such a way that I can sort my data by date?
You can do this by using STR_TO_DATE function in MySQL, try this:
SELECT STR_TO_DATE("17:16:28 Sep 13, 2011 PDT", '%H:%i:%s %b %d, %Y PDT');
EDIT:
SELECT STR_TO_DATE(field_name, '%H:%i:%s %b %d, %Y PDT') AS new_time
FROM table_name;
Use str_to_date with formatting:
select unix_timestamp(str_to_date("17:16:28 Sep 13, 2011 PDT","%T %b %d, %Y PDT"));
If the day part (e.g.: 13) is not represented as 01, 02..etc. but 1, 2, 3 when it's only one digit, change the %d to %e
Be careful because the timezone will not be recognized, it's only a string literal for the formatting! If you have different timezones for different records you should use the convert_tz() function to get the proper timestamp.
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