0

I want to convert date format from 2015-08-01 to 08/01/2015?

I tried this:

DATEADD(DAY, 7 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) as 'Week Ending'

What is the right way to do this?

Bulat
  • 6,869
  • 1
  • 29
  • 52
Holly Cathcart
  • 93
  • 1
  • 1
  • 10
  • 1
    Tip: Formatting data for display is typically done in application code, not in the database. – HABO Aug 10 '15 at 22:06

1 Answers1

0

You will want to use the CONVERT function. The specific format you are looking for is 101.

For example:

CONVERT(VARCHAR(10), DATEADD(DAY, 7 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)), 101) as 'Week Ending'
dub stylee
  • 3,252
  • 5
  • 38
  • 59
  • Excellent. Be sure to upvote and/or accept answer if you found it helpful :) – dub stylee Aug 10 '15 at 21:51
  • 1
    Seriously consider why you are formatting dates in the database. A database is generally not a 'formatting' layer. Generally you should not use it for formatting purposes. – Nick.Mc Aug 11 '15 at 01:08
  • I would also mention that [dates have no format](http://stackoverflow.com/questions/30032915/how-to-cast-the-datetime-to-time/30033028#30033028)... – Zohar Peled Aug 11 '15 at 03:26