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')
Related
I'm replicating from mysql 5.6.33 to 5.7.41. I have a table with a datetime field. If I understand correctly, between 5.6 and 5.7 the decreased the space a datetime field uses because it doesn't store timezone data. (but a timestamp does).
This query works on 5.7 (note the presence of the timezone field):
select count(*) from login_activities where date_created < '2023-01-15 04:00:15 -0800';
This delete statement does not work:
delete from login_activities where date_created < '2023-01-15 04:00:15 -0800'
ERROR:
Error 'Incorrect datetime value: '2023-01-15 04:00:15 -0800' for column 'date_created' at row 1' on query. Default database: 'sms'. Query: 'delete from login_activities where date_created < '2023-01-15 04:00:15 -0800''
How can I get the delete to work in the same way the select works? I've even removed sql_mode entries but still can't get it to work in 5.7
edit:
not sure if this matters, but the error with the delete statement is happining during replication (5.6 -> 5.7), but I'm running the select statement manually. I haven't tried running the delete statement manually because it will through off the replication.
I have a CSV excel file that I want to insert in my MySQL database...but it's giving me an error code on my date.
Dates-Excel :
Error-code-MySQL :
I have tried to set my date in MySQL to datetime but that doesn't work...
you can use str_to_date() function
select str_to_date('2-3-2017','%d-%m-%YYYY')
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
I'm currently migrating a table with dates in VARCHAR columns to a new table with DATE columns. I managed to sanitize the string values in the old table to the format "YYYY-MM-DD" but when I try to perform the insert I got an error with the date "2006-04-31" because that April only had 30 days (was a typo when it was registered),
My question is: how can I set to NULL the column when the date is invalid without getting an error? My SQL is the following:
INSERT INTO newFancyTable (created_at)
SELECT str_to_date(created, '%Y-%m-%d') FROM oldCrappyTable;
And the error is the following:
Error Code: 1292. Incorrect date value: '2006-04-31' for column 'created_at' at row 1
Thanks
UPDATE
I also tried using the following approach:
INSERT INTO newFancyTable (created_at)
SELECT CAST(created AS DATE) FROM oldCrappyTable;
With the same error, and trying to update the oldCrappyTable would return the same:
UPDATE oldCrappyTable SET created = CAST(created AS DATE);
Both return:
Error Code: 1292. Incorrect datetime value: '2006-04-31'
UPDATE 2
At last, I used multiple CASEs to isolate that invalid dates, in sum they were only 5 of them,
Nevertheless, the issue can be reproduced by doing:
CREATE TABLE dates_temp (
test_date DATE DEFAULT NULL
) ENGINE=MEMORY;
INSERT INTO dates_temp
SELECT STR_TO_DATE("2006-04-31", '%Y-%m-%d');
DROP TABLE dates_temp;
A possible workaround is to turn off strict mode, either for the whole server, for a particular session, or for just a few statements. For example:
set #old_sql_mode = ##sql_mode;
set sql_mode = '';
-- Run some statements which may result in error
set sql_mode = #old_sql_mode;
Additional Info
MySQL Documentation
In addition to #EduardoDennis answer use NULLIF to filter out zero dates:
INSERT INTO newFancyTable (created_at)
SELECT nullif(str_to_date(created, '%Y-%m-%d'), from_days(0)) FROM oldCrappyTable;
See my full answer here.
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