SQL Server DateDiff() vs MySQL TimestampDiff() - mysql

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.

Related

Is PHPmyadmin current date based on my computer's localdate?

I'm doing some testing for my system in selecting data between two dates.
so I tried changing my computer's localdate to like year 2020 and run my system, so I'm expecting my CURRENT_DATE is May 10, 2020.
and I wont be getting any rows from my query because all of my data is year 2018
But after I use my query of cur_date() its still selecting those 2018 rows.
so I thought maybe my Phpmyadmin has its own cur_date().
I'm doing this test for my system will be use for the next couple of years. so I want to try and test my queries if today is already 2025 or something.
I thought maybe my Phpmyadmin has its own cur_date().
PhpMyAdmin has nothing to do with it. Your local computer also has nothing to do with it.
When you put CURDATE() in a query, that's part of the query. It's just text, like the SELECT part or the FROM part.
That means it's evaluated by the MySQL server. Just like the data of your rows is retrieved from the server, not from PhpMyAdmin or your local computer.
So the date returned will be that of the MySQL server.
so I want to try and test my queries if today is already 2025 or something.
The way to do this is to take out the expression CURDATE(), and replace it with the "fake" date you wish to use instead.
Something like:
SELECT * FROM `TheTable` WHERE `TheDate` > '2025-01-01';

MySql returns wrong time from table data

I have data in a table, and one of the columns is DATETIME.
select time from tbdt where unix_timestamp(time) > unix_timestamp(now()) order by time asc limit 1
now NodeJS prints wrong time in console.log() like,
actual datetime is 2018-12-16 15:00:00 in db table..
but mysql returns 2018-12-16T09:30:00.000Z
which is 5 hours 30 minutes difference and my time zone is +5:30 (IST)
I don't exactly know where it goes wrong, either in MySql or in Node Js
Need to use convert_tz function in MySql.
SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+5:30');
It has the following signature:
CONVERT_TZ(dt,from_tz,to_tz)
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_convert-tz
You should check the server time on which your MySQL is running as it will pick the time from the server on which it is hosted.
This is being done by the MySQL, not by NodeJS and you can verify the same by directly running your above query into the database by console or SQL developer tool.

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/

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;

Function TO_DAYS mysql on SQL Server

How can this mysql syntax running on SQL server:
SELECT TO_DAYS('2013-04-14')
how TO_DAYS function can running on SQL Server?
Thanks..
Finally, I use
SELECT CAST(CAST('2013-04-14' as datetime) as integer)
Thanks for your answer before
edit: I read the original question as TO_DATE, modified for TO_DAYS
I see TO_DAYS function in MySQL gives the number of days between year 0 and the date parameter.
A basic equivalent in SQL Server is to use DateDiff
e.g.
select Datediff(d,'1900-01-01', '2013-04-14')
Given that there isn't a year 0 in our calendar, I'd argue that the TO_DAYS function is poorly defined.
But assuming that you're using SQL Server 2008 or later, the following should work:
select 366+DATEDIFF(day,'00010101','20130414')
There's no avoiding the "fudge" factor, since SQL Server doesn't accept dates from the year 0 as valid.