HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

Does SQL Server have a Date_Format function?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqlfunctiondate_formatdoesserverhave

Problem

All I found is a list of predefined DateFormat that I can choose from, like this

As I remember in MySQL (and PostgeSQL too?) you can define your Date Format:

DATE_FORMAT(now(),'&m_%Y') --for 02_2012 etc.


Does SQL Server have the same thing? I see people have to write a function to do such thing, does it have a built-in one?

EDIT:

I just found the DatePart function. It can take Month as number, but always returns 1 digit, even I use datePart(MM, getdate())

Solution

Not yet.

You need to use CONVERT with a style parameter or hack something together with DATEPART or DATENAME.

SQL Server 2012 will have the FORMAT function though that accepts a .NET Framework format string

Syntax:

FORMAT ( value, format [, culture ] )


Example Usage

SELECT FORMAT(getdate(), 'dd/MM/yyyy', 'en-US' )


You could always use CLR integration and create your own UDF that does the same thing for 2005 or 2008.

Code Snippets

FORMAT ( value, format [, culture ] )
SELECT FORMAT(getdate(), 'dd/MM/yyyy', 'en-US' )

Context

StackExchange Database Administrators Q#12072, answer score: 16

Revisions (0)

No revisions yet.