I've already exported data from an Oracle 11g instance as insert statements. They need to go into a MySQL 5.5 instance. Having trouble with the TIMESTAMP conversion; I know I can use the TO_TIMESTAMP function inline with INSERT statements in MySQL; however, am unsure as to the correct flags to use. As follows below, in linear order, is: the Oracle timestamp statement and my attempt at a MySQL compatible statement. Obviously it's not working (in that MySQL states there's a syntax error).
Oracle:
TO_TIMESTAMP('12/22/2015 5:08:59.245837 PM','fmMMfm/fmDDfm/YYYY fmHH12fm:MI:SS.FF AM')
MySQL:
TO_TIMESTAMP('12/22/2015 5:08:59.245837 PM','%m/%d/%Y %h:%i:%s')
What am I missing beyond flags for microseconds and AM/PM?
FOR MYSQL:
STR_TO_DATE('2/6/2015 5:20:43.000000 AM','%c/%d/%Y %l:%i:%s.%f %p')
assumed you export from SQL Developer.
before you export the whole data, change your datetime select query with :
TO_CHAR(COLUMN_NAME, 'YYYY-MM-DD HH24:MI:SS') as XXX
it will produce datetime with MySQL format. so when you get the insert query, you can run directly at MySQL
if you want the date and the timestamp of oracle to be compatible with MySql just alter you session
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH:MI:SS.FF'
it is better to alter session
Related
My application can work with both mySql and Oracle db. I've a requirement to add current time in one column( data type :date ) of a transaction table. Oracle has sysdate function but mySql doesnt and mysql has now() function but oracle doesnt.
Is there a way to create single insert statement which will work with both mysql and oracle ?
Both Oracle and MySQL support current_date:
select current_date
If you want the time component as well as the date, use current_timestamp:
select current_timestamp
this query is saving complete date and time. but i want to save only time not date in database. is there any query to do this?
update table set current_time=now();
Your column must be set to either DATETIME or TIMESTAMP.
If you use the TIME type then your query would work as expected.
If you are using any other type of column then you could use CURTIME() method or CAST(column AS TIME) as mentioned by other answers, however this would use more space on disk, and make for much slower queries if you use to select, and prevent you from various operators:
e.g. SELECT * FROM table WHERE current_time<'12:00'
You can see more information about the different DATE column types here: https://dev.mysql.com/doc/refman/5.7/en/date-and-time-types.html
Note that the CURTIME() method is not a standard SQL function, so this would only work on MySql
U can use CONVERT (time, SYSDATETIME()) as the value.
This automates your process without using Current_time=now();
INSERT INTO table SET current_time = CONVERT (time, SYSDATETIME());
You can also use curtime();
INSERT INTO table SET current_time = curtime();
Credits: Salmaan C
I'm using the following query in Oracle, I need the same in MySQL also.
select sys_extract_utc(systimestamp) into currGMTDate from dual;
I tried with CURRENT_TIMESTAMP instead of systimestamp
but the output that I got was a different one. How can I get the same result in MySQL for the above Oracle query?
I tried with the below query and it worked for me
select date_format(CONVERT_TZ( current_timestamp, ##session.time_zone, '+00:00' ),'%d-%m-%Y %r')
I have a table containing datetime field. I want to select records with a condition that only those records that has datetime value above current IST time.
Example
select * from mytable where dt > current_ist_datetime
current_ist_datetime is what i do not know how to use. CONVERT_TZ() with NOW() can give it. But i do not know what timezone mysql uses. that is, i do not know second parameter that i should pass to CONVERT_TZ()
You can retrieve the timezone setting for the current MySQL session with a SQL statement:
mysql> SELECT ##session.time_zone;
If the DBA has not set the timezone on MySQL, it will default to a value of SYSTEM, which represents the timezone on the operating system.
http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
FOLLOWUP:
In a predicate as in your example SQL statement:
select * from mytable where dt > current_ist_datetime
(given dt and current_ist_datetime of datatype DATETIME)
the comparison of the values will be independent of any timezone conversion, since there is no no timezone information associated with a DATETIME value.
That is, the value returned from a column of datatype DATETIME is not affected by the time_zone setting of the MySQL server (SELECT ##global.time_zone) or of the MySQL session (SELECT ##session.time_zone).
The value returned by the NOW() function, however, will be impacted by the time_zone setting of the session.
To get that returned in IST, ensure the time_zone for the session is specified correctly, e.g.
SET VARIABLES time_zone = "+05:30"
(N.B. If the session is retrieved from and returned from a connection pool, the other users of the pool may not be expecting a different time_zone;...)
(NOTE: the above entirely ignores the confusion introduced by the JDBC driver, caused by the "impedance mismatch" (differences) between MySQL's implementation of DATETIME datatype, and Java implementation of Date object. If you are passing DATETIME values across a JDBC connection, that's whole 'nother ball of confusion.)
MySQL uses timezone in two formats; i.e. you can either use 'Asia/Calcutta' or '+05:30' (for IST). But I would suggest you use the latter one.
Timezone for UTC is '+00:00'. So you can use following query provided that dt is in UTC & current_ist_datetime is in IST
select * from mytable where dt > CONVERT_TZ(current_ist_datetime,'+05:30','+00:00')
I am using this query in my project where I paas current_ist_datetime & +05:30 from client(in IST) to my server(in UTC) & it works like a charm.
If you use NOW() mysql will select current date time of your system's timezone
select * from mytable where dt > CONVERT_TZ(NOW(),'+05:30','+00:00')
You can modify these queries according to your need.
How can I get the current timestamp using a mysql query?
Depends on which kind you're looking for.
The current integer Unix Timestamp (1350517005) can be retrieved like so:
SELECT UNIX_TIMESTAMP();
MySQL often displays timestamps as date/time strings. To get one of those, these are your basic options (from the MySQL Date & Time reference):
SELECT CURRENT_TIMESTAMP;
SELECT CURRENT_TIMESTAMP();
SELECT NOW();
CURRENT_TIMESTAMP is standard SQL and works on SQL server, Oracle, MySQL, etc. You should try to keep to the standard as much as you can.
Select current_timestamp;
just use NOW()
Full reference: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html