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
Related
I have migrated a MySQL 5.5 database over to AWS Aurora and have both databases working as expected with one exception. A set of stored procedures that utilise COALESCE and IFNULL in the WHERE clause are not behaving as expected for DATETIME parameters / columns.
The definition of the stored procedure in both MySQL 5.5 and Aurora is:
CREATE DEFINER=`TesterAcct`#`%` PROCEDURE `usp_LookupTest`(
IN paramEmail VARCHAR(200),
IN paramCreateDate DATETIME
)
BEGIN
SELECT
UserID as UserID,
Email as Email,
CreateDate as CreateDate
FROM Users
WHERE
COALESCE(IFNULL(Email,''),'') = COALESCE(paramEmail,IFNULL(Email,''))
AND COALESCE(IFNULL(CreateDate,''),'') = COALESCE(paramCreateDate,IFNULL(CreateDate,''))
;
END
The call being used to test the stored procedure is:
call usp_LookupTest('pcg#none.com',null)
None of the columns for the record being queried are NULL and both database instances (5.5 and Aurora) have exactly the same data and objects
In MySQL 5.5 the record returns as expected and in Aurora an empty dataset is returned. I know it is the CreateDate clause causing the issue because the call in Aurora does return the record when that line is commented out.
I don't believe this SQL syntax is anything non-standard and have not found any documentation on Aurora handling COALESCE or IFNULL differently.
Where should I start looking next?
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 am using Hibernate for ORM mapping. One of the tables has a column of type "datetime". The column in question needs to be updated with "current-time" (time of data insertion). I am aware that I can use the date() function as the default value. I would rather prefer setting the timestamp when I set other attributes of the object.
Question
1) What is the equivalent of SQL date() function in Java?
The equivalent of SQL date() in java is simply new Date(), e.g.:
myEntity.setDateField(new Date());
will set dateField to the current date and time.
With mysql you can set an on update change :
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
The attribut will be updated with the current timestamp of the DB.
(you are lucky because it doesn't work with oracle ... (and I am with oracle ...) )
I am trying in my code:
myEntity.setdDateCreated(new Date());
In hibernate mapping ->
property name="dateCreated" column="date_created" type="timestamp"
But in mysql it is saving as '0000-00-00 00:00:00'
Any guess why this is happening?
Let's say I have rows of information in an SQL database.
I want to have rows of information that are older than 30 days to be automatically removed.
Is this possible?
Additional information:
I am using the SQL date function provided to collect the dates.
This problem has two aspects:
How to schedule
Actual SQL Statement
How to Schedule
You have multiple options, but all of this revolves around whether you will schedule, or whether you will have a stored procedure triggered.
Three options
Use MySql event scheduling: http://dev.mysql.com/doc/refman/5.1/en/events-overview.html
Schedule through an operating system (CRON, SCHEDULED TASK) the execution of an app using SQL (either PHP script, java etc)
Have a trigger that is executes a stored procedure every time a change is made to a table.
I would propose options 1 and 2 are the best.
SQL Statement
The actual SQL statement is quite easy provided you have a field (e.g. dateField) that represents insertDate...
delete from myTable where insertDate < DATE_SUB(NOW(), INTERVAL 1 MONTH);
You can easily generate this value using the NOW() function within an insert statement, for example:
insert into myTable values (NOW(), 'value1', 'value2', ... , 'valueN');
Scheduling Using Event Scheduler at End of Day
If you have the CREATE EVENT privilege this will work through PHP MyAdmin.
See also: http://www.sitepoint.com/how-to-create-mysql-events/
CREATE EVENT `clean_up2`
ON SCHEDULE EVERY 1 DAY STARTS CURRENT_DATE
DO
delete from `data` where updated_on < DATE_SUB(NOW(), INTERVAL 1 MONTH);
END;
Yes. In SQL Server you could create a stored procedure to perform the delete, and create a SQL Agent job that runs every day (or a schedule of your choosing)
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