Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to format getdate into YYYYMMDDHHmmSS

In SQL Server how do I format getdate() output into YYYYMMDDHHmmSS where HH is 24 hour format?

I've got the YYYYMMDD done with

select CONVERT(varchar,GETDATE(),112)

but that is as far as I got.

Thanks.

like image 246
Wayne Avatar asked Sep 23 '11 04:09

Wayne


People also ask

How do I change the 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].


1 Answers

Just for anyone searching for this functionality that has SQL Server 2012 you can use the FORMAT function:

SELECT FORMAT ( GETDATE(), 'yyyyMMddHHmmss') AS 'Custom DateTime'

This allows any .NET format strings making it a useful new addition.

like image 161
Richard Avatar answered Sep 25 '22 08:09

Richard