Portable way to add dates between Oracle and MySQL? - 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)

Related

How to export data from Cloud SQL to BigQuery on a daily basis?

I have created a connection to Cloud SQL and used EXTERNAL_QUERY() to export the data to Bigquery. My problem is that I do not know a computationally efficient way to export a new days data since the Cloud SQL table is not partitioned; however, it does have a date column date_field but it is of the datatype char.
I have tried running the following query with the view of scheduling a similar type so that it inserts the results: SELECT * FROM EXTERNAL_QUERY("connection", "SELECT period FROM table where date_field = cast(current_date() as char);") but it takes very long to run, whereas: SELECT * FROM EXTERNAL_QUERY("connection", "SELECT period FROM table where date_field = '2020-03-20';") is almost instant.
Firstly, it’s highly recommended to convert the ‘date_field’ column to the datatype DATE. This would improve simplicity and performance in the future.
When comparing two strings, MySQL will make use of indexes to speed up the queries. This is executed successfully when defining the string as ‘2020-03-20’ for example. When casting the current date to a string, it’s possible that the characters set used in the comparison aren’t the same, so indexes can’t be used.
You may want to check the characters set once current_datetime has been casted compared to the values in the ‘date_field’ column. You could then use this command instead of cast:
CONVERT(current_date() USING enter_char_sets_here)
Here is the documentation for the different casting functions.

Convert select statement using trunc() and sysdate from Oracle SQL to 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/

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

Declare Mysql variable type outside of procedure

I'm using mysql and writing some queries using SQLYog's query browser. The sql uses a few variables. Below is an example:
SELECT NOW() #cur_dt;
SELECT 'table' INTO #tbl;
SELECT DATABASE() INTO #db;
SELECT ##hostname INTO #host;
SELECT #host AS `host`, #db AS `database`, #tbl AS `table`, #cur_date AS `dt`;
I'm preparing this sql to be used in SSIS 2005(Sql Server Integration Services) as a source. The issue I'm having is that the variables are coming through as blobs instead of varchars or dates.
I can cast each one which works, but my sql above is just a fraction of what I really need which is a bunch of unions. So in the mean time I'm going to wrap a select around all the unions and cast the fields at that point. I know I could put this in a stored procedure and be done with it but I'm wondering about this exact scenario. My question is if there is a way to specify the variable type when declaring?
Thanks,
GG
PS. If you want to punish a developer make them work with SSIS 2005 and mysql.
How about you select all of your data into a table in the mysql query when you have it accumulated? This will give the data reasonable data types. Then use SSIS to interact with the table, not the variables per se.
Example:
http://sqlfiddle.com/#!2/1b059/1

Using a 'date' data type in SQL Server Management Studio 2008

I have a database with existing data that uses the datetime field which is a timestamp that uses date and time.
I want to just store the date, not the time, as the time is never used anyway but due to the way the app works it is causing inconsistencies when reporting on the data.
I believe there is a 'date' data type using the Design view on my table but when I tried to use that it said invalid data type. I also can't do this using the ALTER TABLE syntax in SQL.
Because it's live data, I don't want to make too many changes that might cause problems. We have a demo system that I can play around with though.
SQL server 2008 has a date datatype, which only stores date
the easiest way to do is to alter the table like this:
alter table your_table alter column <dt_col> date
Since you cannot alter the table , I think the only way left is convert while selecting
select convert(date,<dt_col>) from your_table
Or you can create a view with this select statement and use that view in all your queries
SQL fiddle demo