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 TO MM-DD-YYYY HH:MM:SS IN SQL Server?

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

convert yyyy-mm-dd hh:mm:ss to yyyy-mm-dd in sql

I suspect you are overthinking it. A simple cast as date will do the trick

Select Cast(SomeDateTime as date)

SQL Server: Transfer YYYYMMDD-HHMMSS to mm/dd/yyyy hh:mm:ss

Select CONVERT(VARCHAR(25) , CAST(LEFT(ID , 8) AS DATETIME), 101) 
+ ' ' + LEFT(RIGHT(ID , 6) ,2) + ':'
+ SUBSTRING(RIGHT(ID , 6) , 3,2) + ':'
+ RIGHT(ID , 2)
FROM TableName

Convert dd/mm/yyyy hh:mm:ss am/pm to yyyy/mm/dd hh:mm:ss

Why are you starting with a bad regional format? Why do you care about what format SQL Server provides? Let SQL Server interpret the ambiguous regional format and present a proper datetime data type (or, better yet, fix the source so it isn't providing ambiguous formats in the first place). If you want to present it as something like yyyy/mm/dd 24:nn:ss let the presentation tier do that.

That said, you can do this as follows:

DECLARE @BadDateString varchar(32) = '13/10/2021 09:38:20 PM',
@SaneOutput datetime;

SET @SaneOutput = CONVERT(datetime, @BadDateString, 103);

SELECT SaneOutput = @SaneOutput,
BadOutput = CONVERT(char(11), @SaneOutput, 111)
+ CONVERT(char(8), @SaneOutput, 108);

Output:















SaneOutputBadOutput
2021-10-13 21:38:20.0002021/10/13 21:38:20

How to convert YYYY-MM-DD HH:MI:SS format to MM-DD-YYYY HH:MI:SS in MSSQL

Use the FORMAT function:

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

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

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

How to convert "YYYY-MM-DD hh:mm:ss" to "MM/DD/YYYY hh:mm:ss" in R?

For that you need format

format(Sys.time(), "%m/%d/%Y %T")

Or more explicitly

format(Sys.time(), "%m/%d/%Y %H:%M:%S")


Related Topics



Leave a reply



Submit