48

I am using SQL Server 2008.

I have the following:

    select convert(varchar(20),fmdate) from Sery

How do I convert the date to string such that it show as MM/DD/YYYY

Nate Pet
  • 41,226
  • 116
  • 259
  • 398

5 Answers5

79

That task should be done by the next layer up in your software stack. SQL is a data repository, not a presentation system

You can do it with

CONVERT(VARCHAR(10), fmdate(), 101)

But you shouldn't

TFD
  • 22,890
  • 2
  • 33
  • 50
  • 14
    There are actually several reasons to have the database do this, including time/date difference at server location, especially if daylight savings factors into it. Yes, it does force what is likely a locale-specific format, though. – zanlok Apr 02 '13 at 01:42
  • 1
    @zanlok A locale specific format would generally be a bad idea – TFD Apr 02 '13 at 03:12
  • 8
    I would say you don't want to store a formatted date in your database, but if you're for some reason exporting the field (say... to a .csv file) you could have a very valid reason for doing so. – RubberDuck Apr 08 '14 at 13:26
  • 2
    There are plenty of reasons to use this. This time my reason being I am using this to format a date for presentation in body of sp_send_dbmail... – Taylor Brown Aug 02 '17 at 21:26
29
select convert(varchar(10), fmdate, 101) from sery

101 is a style argument.

Rest of 'em can be found here.

T-SQL Cast / Convert date to string

Tony Hopkinson
  • 19,851
  • 3
  • 30
  • 38
4
select convert(varchar(10), cast(fmdate as date), 101) from sery

Without cast I was not getting fmdate converted, so fmdate was a string.

Kirt Carson
  • 716
  • 5
  • 3
3

See this article on SQL Server Helper - SQL Server 2008 Date Format

KyleMit
  • 35,223
  • 60
  • 418
  • 600
Bhanu Pratap
  • 1,453
  • 14
  • 15
3

As of SQL Server 2012+, you can use FORMAT(value, format [, culture ])

Where the format param takes any valid standard format string or custom formatting string

Example:

SELECT FORMAT(GETDATE(), 'MM/dd/yyyy')

Further Reading:

KyleMit
  • 35,223
  • 60
  • 418
  • 600