Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

As title, does it?

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 defines your Date Format:

i.e 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 DatePart function, I can take Month as number, but always 1 digits even I use datePart(MM, getdate())

share|improve this question
    
Possible duplicate of stackoverflow.com/questions/8202257/… –  GSerg Feb 2 '12 at 0:20
1  
Typically, formatting should be handled by the client, not the database. That being said, if you must format the database output, Martin Smith has already provided options. –  JSR Feb 2 '12 at 20:25

2 Answers 2

up vote 12 down vote accepted

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.

share|improve this answer
    
Ahhh, 2012! Oh well, I guess I will write a UDF like you said. Thanks –  King Chan Feb 1 '12 at 21:14
    
That will be awesome. –  Gordon Bell Feb 10 '12 at 16:02

If your goal is simply formatted output and it doesn't need to remain in the datetime datatype, using CONVERT with a format code is an excellent tool. A list of formats and their respective codes can be found here: http://msdn.microsoft.com/en-us/library/ms187928.aspx

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.