views:

288

answers:

5

How can i change dateformat?Forexample:

2009-06-10 10:16:41.123

->2009-June

2009-05-10 10:16:41.123

->2009-May

A: 

Try this:

select cast(datepart(year, mydatecolumn)  as char(4)) + '-' 
   + datename(month, mydatecolumn)
from mytable
ck
+1  A: 

You should not change the date-format in your database. You should just make sure that , when displaying the date, you correctly format the date, so that you display them in the format that you want.

How to do that, is related to the language you use in your program. You can also output the date directly in the format you want using the method of ck.

Frederik Gheysels
I second that. Formatting should never be done in the data layer, it's a localisation nightmare.
Christian Hayter
A: 

heres a link to a table with common formats

almog.ori
A: 

To interpret input use SET DATEFORMAT

To cast to character, see the CONVERT styles.

To format output, use whatever your client environment uses to format output, SQL itself has no output but TDS and display is left to the client.

Remus Rusanu
A: 

Its a pain to do custom formats without writing your own function.

best i have is SELECT NewFormat = YEAR(GETDATE()) + '-' + DATENAME(month, GETDATE())

Rob
You will also have to convert the YEAR to NVARCHAR CAST(YEAR(GETDATE()) AS NVARCHAR(10))
Rob