-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
11
  • You can use format(). Commented May 18, 2018 at 11:21
  • 3
    1. Tag the Sql Server version you are using. 2. Provide the data type of the column you are querying. Commented May 18, 2018 at 11:25
  • Also, change your question from MM-DD-YYYY HH:MM:SS to DD-MM-YYYY HH:MM:SS. No way the required date format matches what you had asked. Commented May 18, 2018 at 11:27
  • It can be done as follows: select CONVERT(VARCHAR(10), GETDATE(), 103) + ' ' + convert(VARCHAR(8), GETDATE(), 14) Commented May 18, 2018 at 11:27
  • Where possible, I would format a datetime data type at the consumer / presentation end, not within the database query Commented May 18, 2018 at 11:31

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Tanveer. Exactly what I was finding.
You Are Welcome, Do use the Links provided for future reference
0

Try this

SELECT FORMAT(CAST('2017-12-18 18:16:49' AS datetime) , 'dd-MM-yyyy HH:mm:ss')

4 Comments

Getting an error 'FORMAT' is not a recognized built-in function name. Severity 15 State 10
@MdKamranAzam i think using lower version sql server 2012
@MdKamranAzam, you should tag your question with the version of SQL Server you are using as the best solution will vary depending on available features.
@Sreenu131. I am using SQL server 2008.
0
DECLARE @date DATETIME = '2017-12-18 18:16:49'

SELECT FORMAT(@date,'dd-MM-yyyy HH:mm:ss')

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.