Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert to Datetime MM/dd/yyyy HH:mm:ss in Sql Server

Tags:

sql-server

How to convert given date format to MM/dd/yyyy HH:mm:ss

I tried this one below but not achieved. Can anyone help me?

SELECT CONVERT(VARCHAR(20), GETDATE(), 120)
like image 720
Naveen Avatar asked Oct 16 '13 17:10

Naveen


People also ask

How do you convert date format from Yyyymmdd to yyyy-mm-dd in SQL?

Convert Char 'yyyymmdd' back to Date data types in SQL Server. Now, convert the Character format 'yyyymmdd' to a Date and DateTime data type using CAST and CONVERT. --A. Cast and Convert datatype DATE: SELECT [CharDate], CAST([CharDate] AS DATE) as 'Date-CAST', CONVERT(DATE,[CharDate]) as 'Date-CONVERT' FROM [dbo].


2 Answers

Supported by SQL Server 2005 and later versions

SELECT CONVERT(VARCHAR(10), GETDATE(), 101) 
       + ' ' + CONVERT(VARCHAR(8), GETDATE(), 108)

* See Microsoft's documentation to understand what the 101 and 108 style codes above mean.

Supported by SQL Server 2012 and later versions

SELECT FORMAT(GETDATE() , 'MM/dd/yyyy HH:mm:ss')

Result

Both of the above methods will return:

10/16/2013 17:00:20
like image 80
M.Ali Avatar answered Oct 18 '22 20:10

M.Ali


Try below:

SELECT CONVERT(VARCHAR(20), GETDATE(), 101)
like image 37
bsivel Avatar answered Oct 18 '22 19:10

bsivel