Saturday, June 6, 2009

How To: Format DateTime in SQL Server

I'll Show You How To Format DataTime in SQL SERVER.


When expression is a date or time data type, style can be one of the values shown in the below Text:

  • 101 >>>>>>>> mm/dd/yy
  • 102 >>>>>>>> yy.mm.dd
  • 103 >>>>>>>> dd/mm/yy
  • 104 >>>>>>>> dd.mm.yy
  • 111 >>>>>>>> yy/mm/dd
  • 114 >>>>>>>> hh:mi:ss:mmm(24h)

more formats

we will assume we have a table contain a DateTime data type field, the Result of the below query will be like:

ex. SELECT BirthDate FROM MyTable

the Result will be ("2/2/1982 1:00:00 AM")

so we can use use Convert Function To Format DateTime

ex. SELECT CONVERT(CHAR(11),GETDATE(),101) BirthDate FROM MyTable

{Result === 06/07/2009 }

ex. SELECT CONVERT(CHAR(11),GETDATE(),114) BirthDate FROM MyTable

{Result === 1:00:00 }

No comments:

Post a Comment