gene
gene

Reputation: 2108

How to convert GetDate() to "YYYY-MM-DD-HHMM" format?

I've been looking for the answer and could not find the exact one satisfying my needs.

I'm looking for the way to convert Datetime value into YYYY-MM-DD-HHMM format.

I tried that one:

select CONVERT(varchar(20),GETDATE(), 120) + replace(convert(varchar(5),getdate(),108),':','')

But it did not give me the right results

Is there any practical way to do it?

Upvotes: 4

Views: 27533

Answers (2)

gene
gene

Reputation: 2108

This works for me:

 select CONVERT(varchar(10),GETDATE(), 20) + '-' + replace(convert(varchar(5),getdate(),108),':','')

I'm not sure if this is efficient or not, though

Upvotes: 0

Siyual
Siyual

Reputation: 16917

In SQL Server 2012 or later you can use the built-in Format() function for this:

Select  Format(GetDate(), N'yyyy-MM-dd-HHmm')

Upvotes: 14

Related Questions