How to convert SQL Server database to MySQL database [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I have created an website using ASP.Net, a table using SQL Server, and I have published it to a provider.
What I want is to convert the MSSQL.sql file to Mysql.sql file. Is there any free software, or some code to convert it to SQL Server to MySQL ?
It would be much appreciated if someone could help me.

If you use phpMyAdmin to manage your MySQL (which almost all web hosts use) you can simply import the file in compatibility mode for MSSQL.
To do this, go to Import -> Choose your file -> Then select "MSSQL" from SQL compatibility mode: under Format specific options.
If you don't have phpMyAdmin already installed, you can download it from the site I linked above for free. The instructions on their website are very clear for installation.
After import, if you want to save a copy of the SQL file in MySQL's syntax simply use the Export feature.

When migrating databases from MS SQL to MySQL server it is often necessary to translate MS SQL queries according to MySQL syntax as well. Syntax of SQL queries in MS SQL and MySQL are similar but not identical. This article discovers 10 most popular differences between MS SQL and MySQL syntax. The target audience for this guide should have general database management knowledge and experience in composing SQL queries.
Sometime MS SQL table or column names are enclosed in square brackets in queries (e.g. if contains spaces or for some other reasons). MySQL does not allow square brackets around table of column names, they all must be replaced by ` symbol or cut off: [object] -> `object`.
MS SQL provides effective solution to avoid naming objects conflict and to manage user permissions on data access. This is schema, a logic container used to group and categorize objects inside the single database. When using schema the full name of database object in query may look like database.schema.object. However, there is no such semantic in MySQL, so all schema names must be cut off from queries.
CONVERT() function is used to convert an expression of one data type to another in MS SQL. In MySQL CONVERT() function converts text data between different character sets. However, there is equivalent function CAST(), so every occurrence of convert(type, expression) in MS SQL query must be replaced by cast(expression AS type) in MySQL query.
LEN() function returns length of string expression in MS SQL. MySQL equivalent for this function is LENGTH().
MS SQL function DATEADD() adds interval to the specified part of the date. MySQL operator '+' can do the same as follows:
DATEADD(year, 1, $date$) -> $date$ + interval 1 year
DATEADD(month, 1, $date$) -> $date$ + interval 1 month
DATEADD(day, 1, $date$) -> $date$ + interval 1 day
where $date$ is an expression of DATE type.
Microsoft SQL and MySQL have different sets of date processing functions, although most of them can be replicated as follows:
DATENAME(month, $date$) -> DATE_FORMAT($date$, '%M') or MONTHNAME(expression)
DATENAME(weekday, $date$) -> DATE_FORMAT($date$, '%W') or DAYNAME(expression)
DATEPART(year, $date$) -> DATE_FORMAT($date$, '%Y')
DATEPART(month, $date$) -> DATE_FORMAT($date$, '%m')
DATEPART(day, $date$) -> DATE_FORMAT($date$, '0')
GETDATE() -> NOW()
GETUTCDATE() -> UTC_TIMESTAMP()
where $date$ is an expression of DATE type.
MS SQL operator '+' allows to concatenate strings like this: 'string1' + 'string2'. In MySQL such expressions must be replaced by CONCAT('string1', 'string2').
MS SQL function CONTAINS(expression, template) searches for matches of template inside expression. MySQL has operator LIKE that implements the same semantics: expression LIKE %template%
If MS SQL query contains 'TOP (100) PERCENT' pattern just cut it off when composing MySQL query. If there is another percentage amount in that pattern, it can be replace by the following code in MySQL (works in MySQL 5.0.7 and higher):
SET #amount =(SELECT COUNT(*) FROM %table name%) * %percentage% / 10;
PREPARE STMT FROM '%original query% FROM %table name% LIMIT ?';
EXECUTE STMT USING #amount;
Syntax of JOIN constructions are very similar in MS SQL and MySQL. The only difference is that MS SQL keyword WHERE is replaced by ON in MySQL. For example:
... table1 CROSS JOIN table2 WHERE condition
must be translated into
... table1 CROSS JOIN table2 ON condition

PHPmyadmin option is nice for doing this job. But sometimes you will see errors while converting. We actually studied the DB structure of the MSSQL and wrote our own mysql statements based on it and did our unit testing and learned few things. So apart from conversion if you also want to do hands-on this is a good approach.

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;

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

MySQL - sql server: consistency check

I'm trying to check the results of a data load between two databases. Unfortunately, I only have access to one database (MySQL) directly, the company managing MSSQL can expose it to us via an API.
What I would like to do is check the consistency of certain columns across rowsets. Originally, I had hoped to be able to run a CRC or hash check against the columns, but there doesn't seem to be a compatible way of doing this.
For example, we can run CRC32 against a column in MySQL, but there isn't a reliable way of doing the same on MSSQL. Alternatively, there's CHECKSUM_AGG on MSSQL, but no alternative on MySQL.
The end result is that I would like to do a binary search if the checksums differ to identify the rows that require changing.
There is currently no bulk load interface, and SSIS is not available (the MSSQL servers are not part of my company).
I thought I'd come back to this and describe the solution we ended up implementing. This was a major pain in the neck!
Firstly, because of the fixed versions of MySQL on our server and MSSQL on the remote server, there were no common encoding methods.
The MSSQL API returned data in UTF-16LE, the MySQL database had Unicode data stored in Latin-1 tables sigh
Firstly, we concatenated the fields that we were comparing, then we MD5'd the result. In order to get the MySQL result to match the output of the MSSQL HASHBYTES function, we had to do this:
SELECT ABS(CONV(CONCAT(
IF(MID(MD5(CONC), -8 , 1) >= "8", "FFFFFFFF", ""),
RIGHT(MD5(CONC), 8)
), 16, -10 )) AS CALC
where CONC is the result of a subselect concatenating the fields we are interested in.
On the MSSQL server, we had to do the following query:
SELECT ABS(CONVERT(INT,HASHBYTES('MD5',
CONVERT(NVARCHAR(4000), FIELD1 ) +
CONVERT(NVARCHAR(4000), FIELD2 ) + ...
Then, we took the sum of the MD5 across the entire range, modulo three large-ish primes(311,313,317), as per Chinese Remainder Theorem
This gave us three numbers for the range we were checking. We could be reasonably certain that if all three numbers matched for a given range on each server, then the data was consistent.
I'll spare you the details of the munging we had to do to get Unicode in Latin-1 transliterated to UTF-16LE