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:
SaneOutput | BadOutput |
---|---|
2021-10-13 21:38:20.000 | 2021/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
How to Decrypt MySQL Passwords
How to Limit Results of a Left Join
How to Select Rows With No Matching Entry in Another Table
Multiple Rows to One Comma-Separated Value Different Id
Multiple Rows Output into Variables in MySQL
Sql Select All Data That Past 45 Days
How to Get Total Count Value Each Day Upto 5 Days
Comparing Two Columns Using SQL to Make Sure the Same Set of Values Are Present in Both
Find Out Count of Employees Joined in January Month
How to Make an If Statement in Ssis
Postgres Query to Check a String Is a Number
Sql 0 Results for 'Not In' and 'In' When Row Does Exist
Deleting a Specific Character in a String from a Pattern
Sql Get Parent Where Children Have Specific Values
Multiplying Two Columns in SQL Server
Checking a Column If It Contains a Row Value