I have a table alert_log has column name date_dt (type varchar 50) which stores unix timestamp data
example data : 1518783503000
select FROM_UNIXTIME(date_dt) AS 'date_formatted' from alert_log
it always returns null.please help i would like to convert the example unix data as Fri Feb 16 2018
That is because 1518783503000 is not a valid MySQL unix time.
Divide by 1000 to get it right because unix time is in seconds and you stored it in milliseconds.
select FROM_UNIXTIME(date_dt / 1000) AS 'date_formatted'
from alert_log
Related
I have a query selecting time in the format where the time column is of the DateTime datatype.
SELECT ul.time FROM user_logins;
It is returning the result in this format-
11/27/2021 9:29:46 AM
11/23/2021 12:48:20 PM
Now I want it to return in the 24 hour format like
11/27/2021 9:29:46 11/23/2021 00:48:20
Is there any possible way to achieve that?
DATETIME values always stored in 24 hour format. As your column defined as DATETIME datatype, it's in 24 hour format
Output of DATETIME datatype:
2021-12-07 18:21:30.907
AM / PM is not mentioned by DATETIME, So We can format it.
Query: Select CONVERT(varchar,GETDATE(),9)
Output: Dec 7 2021 7:26:55:077PM
Unix time stamp conversion giving tow different result in mysql and oracle
select FROM_UNIXTIME(1387444958) from dual;
Output :2013-12-19 10:22:38
select to_char(to_date('01/01/1970 00:00:00','DD/MM/YYYY HH24:MI:SS')+ (1387444958/86400),'YYYY-MM-DD HH24:MI:SS')from dual;
output: 2013-12-19 09:22:38
Can anyone please help me in getting same timestamp from oracle as I am getting in MySql.
Unix timestamp is seconds from 1970-01-01 00:00:00 UTC which is actually 1970-01-01 01:00:00 in your local timezone (or the timezone where your MySQL server is located). Looks like FROM_UNIXTIME takes this into account.
For Oracle you can use this function:
FUNCTION UnixTime2Timestamp(UnixTime IN NUMBER) RETURN TIMESTAMP IS
BEGIN
RETURN (TIMESTAMP '1970-01-01 00:00:00 UTC' + UnixTime * INTERVAL '1' SECOND) AT LOCAL;
END UnixTime2Timestamp;
I assume if you like to get UTC time in MySQL then you have to run
select
CONVERT_TZ(FROM_UNIXTIME(1387444958),'{your local timezone}','UTC')
from dual;
Unix time stamp conversion giving tow different result in mysql and oracle
select FROM_UNIXTIME(1387444958) from dual;
Output :2013-12-19 10:22:38
select to_char(to_date('01/01/1970 00:00:00','DD/MM/YYYY HH24:MI:SS')+ (1387444958/86400),'YYYY-MM-DD HH24:MI:SS')from dual;
output: 2013-12-19 09:22:38
Can anyone please help me in getting same timestamp from oracle as I am getting in MySql.
Unix timestamp is seconds from 1970-01-01 00:00:00 UTC which is actually 1970-01-01 01:00:00 in your local timezone (or the timezone where your MySQL server is located). Looks like FROM_UNIXTIME takes this into account.
For Oracle you can use this function:
FUNCTION UnixTime2Timestamp(UnixTime IN NUMBER) RETURN TIMESTAMP IS
BEGIN
RETURN (TIMESTAMP '1970-01-01 00:00:00 UTC' + UnixTime * INTERVAL '1' SECOND) AT LOCAL;
END UnixTime2Timestamp;
I assume if you like to get UTC time in MySQL then you have to run
select
CONVERT_TZ(FROM_UNIXTIME(1387444958),'{your local timezone}','UTC')
from dual;
We have a database with all the dates and times stored in one column called timestamp. The format of the date/time in the column "timestamp" is as: 03 Aug 08:10am.
I would like to convert this (03 Aug 08:10am) to UNIX TIMESTAMP in MySQL and not PHP because we already have over 500 rows with this format: 03 Aug 08:10am.
I tried create a new INT column called new_timestamp and ran this query:
UPDATE table_name SET new_timestamp = UNIX_TIMESTAMP(timestamp);
However, it shows that 0 rows were affected.
This is not a duplicate, don't redirect me to how to convert in PHP. Read the question first :)
The UNIX_TIMESTAMP() function requires a valid date/time format to convert correctly, so you need to convert your existing date/time format to a valid/recognised format (including the year) first. You can do this using MySQL's STR_TO_DATE() function, telling it what format you are passing in, and concatenating in a hard-coded year value as it's always 2016 in your case.
STR_TO_DATE(CONCAT('2016-', <your date/time value>), '%Y-%d %b %h:%i%p')
You can then use the UNIX_TIMESTAMP() function to convert that valid date to your unix timestamp and update all those records in a single step:
UPDATE table_name
SET new_timestamp =
UNIX_TIMESTAMP(STR_TO_DATE(CONCAT('2016-', timestamp), '%Y-%d %b %h:%i%p'));
I am trying to import data from a csv file to a mysql table. One of the columns contains a serialized date (it came from another mysql table) which I need to convert to a format yyyy-mm-dd hh:mm:ss.000000. An example serial date is "1389792682". I tried converting in Excel but none of the custom formats recognize the number as a date - I believe because it's a date-time number. Any ideas?
Thanks
I think that you can use this formula in order to convert this timestamp to a date. Supposing your timestamp is in A1 cell:
=((A1/3600)/24) + DATE(1970,1,1)
This is because Unix epoch is the time 00:00:00 UTC on 1 January 1970.
And then chose to format your cell as a date.
The timestamp that you specifies in your question gives me:
year: 2014
month: 01
day: 15
h: 13:31
1389792682 corresponds to 2014-01-15 05:31:22 -0800 (2014-01-15 13:31:22 UTC) in Unix/Linux time.
It can be converted to a date field with
DATE_ADD('1970-01-01', INTERVAL 1389792682 SECONDS)