Unix time stamp conversion is different in Mysql and Oracle - mysql

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;

Related

Convert MySQL date time 24 hour format to UNIX timestamp

I'm storing date time in 04-09-2019 10:31:AM(4 September 2019 10:31 AM) in this format in MySQL table.
May I know how to convert this format to unixtime stamp in sql query
First you need to convert this into a proper mysql datetime using str_to_date() function.
str_to_date(dd, '%d-%m-%Y %h:%i:%p')
Then after getting the correct datetime value, use unix_timestamp() function to convert it to unix timestamp.
select
unix_timestamp(str_to_date(dd, '%d-%m-%Y %h:%i:%p'))
from test
try this dbfiddle.

MySQL query comparing NOW() with ISO string

I would like some confirmation on how NOW function works in MySQL
According to the docs and when runing the this query SELECT NOW() the mysql returns the current time (local time i guess?) in following format YYYY-MM-DD HH-MM-SS.
If this is the case, then how come this works when comparing NOW() to a column that includes a UTC ISO date and time?
For example this works fine:
SELECT * FROM table where deadline > NOW() # deadline column contains a utc ISO string
Is the query above reliable or did it just return the correct answer by luck?
in case this is NOT reliable, how would you do the comparison?
MySQL NOW()-function returns a datetime in the session timezone. MySQL has UTC_TIMESTAMP()-function which returns current UTC date and time, which will work better when you are compare it to an UTC date time.
Note that you should store datetimes as DATETIME, instead of char/varchar (assume this is what you meant by "UTC ISO date and time").

Getting Different timestamp when Converting same Unix time in Two Different DB [duplicate]

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;

UTC to America/New_York time zone using MySQL

I am storing a date in UTC timezone inside my table but my need is to export a CSV file by converting a date from UTC to America/New_York time zone.
I have tried with CONVERT_TZ MySQL function, but it's giving null.
How I can convert a date from UTC to America/New_York time zone using only MySQL and not using PHP?
You have to load the timezone table.
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p password
If you do not have superuser access, you can skip friendly timezone names and specify the hours.
CONVERT_TZ(date,'+00:00','-07:00')
The reason behind you are getting null result once you are using CONVERT_TZ MySQL function is that TZ time zone table have not been setup.
You can check that time zone table is set up or not.
select * from mysql.time_zone;
If it's giving null values then you need:
insert time zone in MySql if you want to use CONVERT_TZ MySQL function to convert a date from UTC to America New_Yark time zone.
You can run below query without update time_zone table.
SELECT DATE_SUB( order_date, INTERVAL 5 HOUR ) as OrderDate
FROM TABLE_NAME
With date format:
SELECT DATE_FORMAT( DATE_SUB( order_date, INTERVAL 5 HOUR ) , '%Y-%m-%d %h.%i.%s %p' ) as OrderDate
FROM TABLE_NAME
Please have a look similar question.
MySQL CONVERT_TZ()
It will help you in "How to insert timezone in MySql".

MySQL substract two datetimes and returns in nanoseconds

I have 2 datetime columns in a MySQL table.
I want to subtract it and return the results and a nanoseconds precision
This is my code so far.
select end,start,end-start from job where id=1;
I have the results like this
'2014-04-02 12:30:00', '2014-04-02 10:30:00', 20000.000000
Just for curiosity how MySQL subtract directly, how 20000.000000 came from.
I am not good handling dates. Which is the best approach?
unix_timestamp: If you want to convert a UNIX TIMESTAMP into seconds since '1970-01-01'
If called with no argument, returns a Unix timestamp (seconds since
'1970-01-01 00:00:00' UTC) as an unsigned integer. If UNIX_TIMESTAMP()
is called with a date argument, it returns the value of the argument
as seconds since '1970-01-01 00:00:00' UTC. date may be a DATE string,
a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or
YYYYMMDD.
Consider this :
SELECT unix_timestamp(now()) - unix_timestamp('2007-03-19 09:50:00')
You want this:
SELECT unix_timestamp(column1) - unix_timestamp(column2) from tableName
See it in action
SELECT start_time,
end_time,
( Unix_timestamp(start_time) - Unix_timestamp(end_time) ) * 1000000000 AS
NanoSeconds
FROM job
Output:
start_time | end_time | NanoSeconds
April, 02 2014 12:30:00 April, 02 2014 10:30:00 7200000000000
As we can format this datetime column value also with DATE_FORMAT(). TO_SECONDS() is available beginning with MySQL 5.5.0.
MYSQL - datetime to seconds also gives some lightness on another solution of the output you want.