How to convert MSSQL date format to MySQL

I recently had to transfer over some databases from MSSQL Server 2000 to MySQL. Unfortunately, the date format that MSSQL keeps is MM-DD-YYYY HH:MM:SS am/pm. MySQL uses YYYY-MM-DD HH:MM:SS (24hour format). For example, 8/14/2007 5:00:00 PM (MSSQL format) and 2007-08-14 17:00:00 (MySQL format). In MySQL, we can use the date_format(). But what is the equivalent for MSSQL? That would be the CONVERT(). This is how you would use it.

Here’s the query to run on MSSQL:

SELECT CONVERT(VARCHAR(40),[date_column_name],120) AS new_date_format FROM [table_name]

The “new_date_format” is the column name that will display the MySQL formatted date. This should also work if you have a column that stores datestamp and a column that stores timestamp.

You can find a list of expression values from http://msdn2.microsoft.com/en-us/library/ms187928.aspx. Hope this helps somebody.

Similar Posts

One Comment

  1. thank you, i love you!I have been trying to figure this out for about 5 hours, your answer is so easy and works perfectly.

Leave a Reply

Your email address will not be published. Required fields are marked *