I am migrating DB from MySQL to Oracle, so need to write similar query in oracle.
Following is the MySQL query, how to convert this to Oracle:
SELECT convert_tz(datetime, Etc/UTC, America/Los_Angeles) AS datetime1 from XYZ
select FROM_TZ(TIMESTAMP '2005-09-11 01:50:42', '5:00')
from dual;
timestamp_value
The value to convert to a TIMESTAMP WITH TIME ZONE value.
time_zone_value
The time zone used to convert timestamp_value to a TIMESTAMP WITH TIME ZONE value.
11-Sep-05 01.50.42.000000 AM +05:00
Related
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").
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".
I am trying to insert '2009-03-04T17:49:20Z' which is in zulu time into datetime type column of mysql table.
Any idea how I can do that?
Use str_to_date() to parse your date format like this:
set myDate = str_to_date('2009-03-04T17:49:20Z', '%Y-%m-%dT%H:%i:%sZ')
I've a timestamp field in my database in which data is inserted from python datetime module. I want to convert the timestamp to date and do some filtering. I can do this in python but instead I want to do this in sql query itself.
I tried this so far
Select DATE_FORMAT(FROM_UNIXTIME('ts'), '%Y %b %e') AS 'date_formatted' from tablename limit 100;
This is converting to date but the from_unixtime is messing it to dates in 1969 instead of 2013.
I need to format python's timestamp in mysql.
Is ts the column name of the timestamp? If so, why are you putting it in single quotes? That makes it into a literal string.
MySQL date functions translate a literal string that isn't a timestamp, silently, into NULL. A NULL or zero UNIX timestamp refers to the beginning of the UNIX epoch, which is 1-Jan-1970 at midnight universal time. That's 19:00 on 31-Dec-1969 New York time. That is, I believe, where your 1969 stuff is coming from.
I wasn't able to find out (googling, reading mysql reference manual) how to get value of DATETIME in seconds in MySQL.
I dont mean to extract seconds from datetime, but to convert it into seconds.
If by "convert to seconds", you mean "convert to an UNIX Timestamp" (i.e. number of seconds since 1970-01-01), then you can use the UNIX_TIMESTAMP function :
select UNIX_TIMESTAMP(your_datetime_field)
from your_table
where ...
And, for the sake of completness, to convert from an Unix Timestamp to a datetime, you can use the FROM_UNIXTIME function.
If you want to have the difference between two DATETIME values, use TIMESTAMPDIFF:
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
Returns datetime_expr2 – datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part '00:00:00' where necessary. The unit for the result (an integer) is given by the unit argument. The legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function.
mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
-> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
-> -1
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
-> 128885
unit can also be HOUR which is what you asked for in one of the comments.
The unit argument can be any of the following:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
The level of usefulness of some of the other options will of course be determined by the granularity of the data. For instance, "MICROSECOND" will only have limited use if you are not storing microseconds in your DATETIME values.
Use TIME_TO_SEC in previous versions for mysql
SELECT TIME_TO_SEC(time column) FROM table
i used in mysql
TO_SECONDS(your date goes here) method to convert date to seconds from year 0
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
The function UNIX_TIMESTAMP(datetime) returns the unix time, which happens to be the number of seconds since 1-Jan-1970 0000 UTC. That may be what you need, but not if you're dealing with dates of birth, historical dates, or dates after 2037.
Starting in mysql 5.5.0 you can use to_seconds()
TO_SECONDS(FIELD_NAME)
FIELD_NAME must be DATETIME type
I have created my own query for your problem:
SELECT HOUR(`colname`) * 3600 + MINUTE(`colname`) * 60 + SECOND(`colname`)
FROM widgets
WHERE id = 1;
Use id = 1 if you have to take a specific row.
The output will be in seconds.