|
Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - CONVERT() - Formatting DATETIME Values to Strings
By: FYIcenter.com
(Continued from previous topic...)
How To Format DATETIME Values to Strings with the CONVERT() Function?
SQL Server 2005 offers no functions to format DATETIME values in your own format patterns.
But it does provide you a number of pre-defined format patterns that you can use with the
CONVERT(char_type, date, pattern_code) function. Commonly used pattern codes are:
Code Name Format
100 Default mon dd yyyy hh:miAM/PM
101 U.S. mm/dd/yyyy
102 ANSI yyyy.mm.dd
103 British/French dd/mm/yyyy
104 German dd.mm.yy
105 Italian dd-mm-yy
107 Mon dd, yyyy
108 hh:mi:ss
110 USA mm-dd-yy
111 Japan yy/mm/dd
121 ODBC canonical yyyy-mm-dd hh:mi:ss.mmm
For examples, see the tutorial exercise below:
DECLARE @birth_date DATETIME;
SET @birth_date = '1987-05-19 16:10:41.403';
SELECT 'You were born on '
+ CONVERT(VARCHAR(40),@birth_date,107)
+ ', at '
+ CONVERT(VARCHAR(40),@birth_date,108);
GO
You were born on May 19, 1987, at 16:10:41
(Continued on next topic...)
- How To Add or Remove Days on Date and Time Values?
- Can Date and Time Values Be Converted into Integers?
- Can Integers Be Converted into Date and Time Values?
- Are DATETIME and NUMERIC Values Convertible?
- Can a DATETIME Value Be Subtracted from Another DATETIME Value?
- What Are the Date and Time Functions Supported by SQL Server 2005?
- How To Increment or Decrement Parts of DATETIME Values?
- How To Use DATEADD() Function?
- How To Calculate DATETIME Value Differences Using the DATEDIFF() Function?
- How To Calculate Age in Days, Hours and Minutes?
- How To Get Month and Weekday Names from DATETIME Values?
- How To Get Parts of DATETIME Values as Integers?
- How To Get Year, Month and Day Out of DATETIME Values?
- What Is the Difference Between GETDATE() and GETUTCDATE()?
- How To Format Time Zone in +/-hh:mm Format?
- How To Format DATETIME Values to Strings with the CONVERT() Function?
- How To Truncate DATETIME Values to Dates without Time?
- How To Set Different Parts of a DATETIME Value?
|