How to format DateTime 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)
we will assume that we have a table contain a DateTime data type field, the result of the below query will be like:
SELECT BirthDate FROM MyTable
The result would be (“2/2/1982 1:00:00 AM”)
We can use Convert Function To Format DateTime
SELECT CONVERT(CHAR(11),GETDATE(),101) BirthDate FROM MyTable
The result would be (“06/07/2009”)
SELECT CONVERT(CHAR(11),GETDATE(),114) BirthDate FROM MyTable
The result would be (“1:00:00”)
Leave a Reply