Search code examples
sqlsql-serversql-server-2008datetimesql-server-2008-express

How to convert YYYY-MM-DD HH:MM:SS TO MM-DD-YYYY HH:MM:SS IN SQL Server?


How to convert yyyy-mm-dd hh:mm:ss (in 24 hours format) to dd-mm-yyyy hh:mm:ss (in 24 hours format? I am using Sql Server 2008.

Given Date Format: 2017-12-18 18:16:49 - Its in DateTime format
Required Date Format: 18-12-2017 18:16:49

Solution

  • This Would work in Older SQL Server Versions Also, Converted to datetime first if it's VARCHAR otherwise you can skip that conversion.

    SELECT convert(varchar,convert(datetime,'2017-12-18 18:16:49'),105) + ' ' + 
    convert(varchar(8),convert(datetime,'2017-12-18 18:16:49'),14);
    

    Use this Link as Reference for Date & Time conversion Formats