gtludwig
gtludwig

Reputation: 5611

how to format a SQL Server date according to a specific pattern?

In MySQL there's the DATE_FORMAT() function that allows to represent a given date value according to the specified pattern.

Is there something like it in MS SQL Server?

The statement I'm trying to format is:

SELECT TOP 1 CONVERT(VARCHAR(20), ot.Timestamp,110) AS 'TS' FROM ot, cl WHERE ot.CompID = cl.CompID;

Which outputs as MM-DD-YYYY. There are other styles, but I haven't found one that suits me.

I need something that outputs as DD/MM/YYYY HH:MM. Where HH is 24 hour format.

How can I best do this?

Upvotes: 2

Views: 3360

Answers (2)

Anda Iancu
Anda Iancu

Reputation: 530

From SQL Server 2012 you can use the FORMAT function.

 SELECT getdate(), FORMAT (getdate(), ' dd/MM/yyyy HH:mm').

http://www.sql-server-helper.com/sql-server-2012/format-function-vs-convert-function.aspx

Upvotes: 3

Daniel Kelley
Daniel Kelley

Reputation: 7737

See MSDN. Looks like you can use the following:

select convert(varchar(10), getdate(), 103) + ' ' + convert(varchar(8), getdate(), 108)

Upvotes: 1

Related Questions