0% found this document useful (1 vote)
214 views

In SQL Server Used Cast or Convert Function To Format DateTime Value or Column Into A Specific Date Format

The CAST and CONVERT functions in SQL Server are used to format datetime values or columns into specific date formats. CAST converts a datetime to a varchar or string. CONVERT changes or converts datetime formats and can return just the date or time portion. CONVERT takes a datetime style parameter to specify the desired format like mm/dd/yy, dd/mm/yyyy, or yyyymmdd. Examples show using CONVERT with GETDATE() and different style parameters to return formatted datetime strings.

Uploaded by

Umakant Parashar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
214 views

In SQL Server Used Cast or Convert Function To Format DateTime Value or Column Into A Specific Date Format

The CAST and CONVERT functions in SQL Server are used to format datetime values or columns into specific date formats. CAST converts a datetime to a varchar or string. CONVERT changes or converts datetime formats and can return just the date or time portion. CONVERT takes a datetime style parameter to specify the desired format like mm/dd/yy, dd/mm/yyyy, or yyyymmdd. Examples show using CONVERT with GETDATE() and different style parameters to return formatted datetime strings.

Uploaded by

Umakant Parashar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

In SQL Server used Cast or Convert function to Format DateTime value or column into a specific date format.

Both function are used to convert datetime to varchar or string. CAST function Syntax: CAST(expression as data_type) Let's convert current date time to varchar select cast(getdate() as varchar) CONVERT function is used to change or convert the DateTime formats.By using convert function you can get only Date part or only Time part from the datetime. CONVERT Function Syntax: CONVERT(data_type,expression,date Format style) Let's take Sql Server DateTtime styles example: Format USA mm/dd/yy ANSI yy.mm.dd British/French dd/mm/yy German dd.mm.yy Italian dd-mm-yy dd mon yy Mon dd, yy USA mm-dd-yy JAPAN yy/mm/dd ISO yymmdd mon dd yyyy hh:miAM (or PM) mm/dd/yyyy yyyy.mm.dd dd/mm/yyyy dd.mm.yyyy dd-mm-yyyy dd mon yyyy Mon dd, yyyy hh:mm:ss Default + milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM) mm-dd-yyyy yyyy/mm/dd yyyymmdd Europe default + milliseconds dd mon yyyy hh:mm:ss:mmm(24h) hh:mi:ss:mmm(24h) Query select convert(varchar, getdate(), 1) select convert(varchar, getdate(), 2) select convert(varchar, getdate(), 3) select convert(varchar, getdate(), 4) select convert(varchar, getdate(), 5) select convert(varchar, getdate(), 6) select convert(varchar, getdate(), 7) select convert(varchar, getdate(), 10) select convert(varchar, getdate(), 11) select convert(varchar, getdate(), 12) select convert(varchar, getdate(), 100) select convert(varchar, getdate(), 101) select convert(varchar, getdate(), 102) select convert(varchar, getdate(), 103) select convert(varchar, getdate(), 104) select convert(varchar, getdate(), 105) select convert(varchar, getdate(), 106) select convert(varchar, getdate(), 107) select convert(varchar, getdate(), 108) select convert(varchar, getdate(), 109) select convert(varchar, getdate(), 110) select convert(varchar, getdate(), 111) select convert(varchar, getdate(), 112) select convert(varchar, getdate(), 113) or select convert(varchar, getdate(), 13) select convert(varchar, getdate(), 114)

You might also like