-1

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
Md Kamran Azam
  • 129
  • 2
  • 17

4 Answers4

0

Try this

SELECT FORMAT(CAST('2017-12-18 18:16:49' AS datetime) , 'dd-MM-yyyy HH:mm:ss')
Sreenu131
  • 2,476
  • 1
  • 7
  • 18
0
DECLARE @date DATETIME = '2017-12-18 18:16:49'

SELECT FORMAT(@date,'dd-MM-yyyy HH:mm:ss')
Sreenu131
  • 2,476
  • 1
  • 7
  • 18
Sameer
  • 349
  • 4
  • 12
0

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

0

I would advice not change directly in SQL formate, instead you could change your php code where query fetch the date data

for example you could assign your $yourDate object to new dateTime, and use formate function to define the date format:

<?php
$yourDate = new DateTime();
$timestring = $yourDate->format('m-d-Y h:i:s');
echo $timestring;

the output will be: 05-18-2018 05:00:34

You could take reference in this older discussion

This will prevent some bug error might be occurred if your data table has some other date format relative to, and also it is more useful once you need to have change the date format again

Hope this will be help

tess hsu
  • 91
  • 1
  • 8