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?

1
  • 1
    Tip: Formatting data for display is typically done in application code, not in the database.
    – HABO
    Aug 10, 2015 at 22:06

1 Answer 1

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'
3
  • Excellent. Be sure to upvote and/or accept answer if you found it helpful :)
    – dub stylee
    Aug 10, 2015 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, 2015 at 1:08
  • I would also mention that dates have no format... Aug 11, 2015 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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