Convert select statement using trunc() and sysdate from Oracle SQL to MySQL - mysql

This below Oracle query returns dates between last day of previous month till today. I need same results in MySQL. Can anybody help me to write the query in MySQL?
Please note that I have drive this query on 'DUAL'. There is no physical/actual table.
SELECT TRUNC(TRUNC(TRUNC(sysdate,'MM')-1)+level)-1 attendance_date
FROM dual
CONNECT BY level<= (TRUNC(sysdate)-TRUNC(sysdate,'mm'))+2;

MySQL simply doesn't have the nonstandard Oracle CONNECT BY feature. It doesn't yet have the standard recursive common table expression feature. Your present approach to your problem isn't the right one to get a sequence of dates.
There are other ways to to get a sequence of dates.
MySQL how to fill missing dates in range?
http://www.plumislandmedia.net/mysql/filling-missing-data-sequences-cardinal-integers/

Related

Proper way to use datetime in Codeigniter queries

I wonder if there is some type of common method that would help me write query with date/time field in it. For example: I am developing a very small project utilizing MySQL database. However, my client is considering switching to his existing SQL server.
Example (datetime column):
SELECT DATE_FORMAT(contract_date, '%d.%m.%Y') FROM `employees`
Question: Can query below become usable in SQL in case I replace database driver (currently) mysqli to sqlsrv?
I understand I can use some type of config variable for date format... Would it be the best way? Is there something that Codeigniter 3 has in place?
feel free to use your own query sample

SQL Server DateDiff() vs MySQL TimestampDiff()

I have migrated my application's database from SQL Server to MySQL. Now I'm adjusting my application code and I'm running into issues with date functions. Specifically, it seems like SQL Server's DateDiff() rounds up while MySQL's TimestampDiff() rounds down. For example:
SQL Server: select datediff(day,'2015-11-25 12:00:00', '2015-11-26') returns 1
MySQL: select timestampdiff(day,'2015-11-25 12:00:00', '2015-11-26') returns 0
What would be the best way to make MySQL return the same results as SQL Server? I can't just add 1 to each diff expression in MySQL because in cases where the difference between date1 and date2 are exactly X days apart, the MySQL evaluates exactly as SQL Server evaluates. For example:
SQL Server: select datediff(day,'2015-11-25', '2015-11-26') returns 1
MySQL: select timestampdiff(day,'2015-11-25', '2015-11-26') returns 1
EDIT: Comments are only suggesting conversions for differences in DAYs. I will also need to support differences in SECOND, WEEK, MONTH, YEAR, etc.
If I were doing this I would write a stored function SQL_SERVER_DATEDIFF() as a wrapper around MySQL TIMESTAMPDIFF() with adjustments to make it behave like SQL Server DATEDIFF() and do a search/replace through the code. This gives you the flexibility to fix this issue as well as any others that might arise in the future.

SQL Server: date incompatible with int when migrating from mysql. How to solve?

I have similar databases, they come from the same CMS but they use different databases: some are originally SQL Server, and another one is MySQL.
I had to migrate the MySQL database to SQL server since I have some scripts ready for SQL Server which a. a don't want to convert, b. are more complicated to convert since some functions I use are not implemented in MySQL.
This query on the database which were originally SQL Server runs without problems:
SELECT Birth_Date+1 FROM TABLENAME
while, when I run it on the same table in the database I migrated from SQL, I get this error:
Operand type clash: date is incompatible with int
Any idea why I get this error and how I can solve it?
I migrated my database with SSMA, SQL Server Migration Assistent, if this can help.
Thank you.
The message is rather clear, you can't use the + operator with a date and an int.
You should use the DATEADD function (in Sql Server)
And DATE_ADD in mysql.
DATE_ADD(Birth_Date, interval 1 DAY) for example.
which is
DATEADD(day, 1, Birth_Date) in Sql Server
The error seems pretty clear. You can't add an integer to a date, although you can add an integer to a datetime. Presumably, the data type of Birth_Date is date in one database and datetime (or something similar) in the other.
Here are two solutions:
SELECT cast(Birth_Date as datetime)+1 FROM TABLENAME;
SELECT dateadd(day, 1, Birth_Date) FROM TABLENAME;

Compatibility Query for inserting null values in datefield

I'm using Oracle and MySQL database in our project. So we are trying to write common queries for both databases.
I'm trying to insert a date in a date field into both database tables. Both databases support this date format only: 2013-07-19. In the course of our DML operations we are facing a problem when inserting dates as Empty or Null.
Both databases have their own syntax to store an "empty field" in a Date field/column.
MySQL allows dates as 0000-00-00 or NULL (specifically written at an appropriate position). But Oracle does not support this format. Oracle only allows date fields as Empty.
How to write common queries in this type of situation?
INSERT INTO TABLENAME(DATEFIELD) VALUES(NULL);
...works both in MySQL and Oracle, so you could use that.

Portable way to add dates between Oracle and MySQL?

Is there any way to add dates/times that will be portable between Oracle and MySQL?
For example, in Oracle, adding col + 1 to a date column will add a day.
In MySQL, adding col + 1 to a datetime column will add a SECOND.
Is there a function that would give the same results in both?
(I'm trying to use this in an order by, for example, order by col1 + col2/(60*24) - if it were part of the SELECT or WHERE, there might be better options.)
Thanks!
select datecolumn + interval '1' day
from your_table
works with Oracle and MySQL
If you're looking to create an app that will work on oracle and MySQL using the same set of queries, did you consider shipping a script with it that will create stored procedures to do the minor work you require e.g. creating a DATE_ADD_DAYS procedure that you then call. The sproc contains the db specific stuff and your app contains only calls to these sprocs you know will exist (because they're in your install script)