How to convert the date format using the convert function in SQLserver

Source: Internet
Author: User

The default datetime format of the Chinese version of SQL Server is yyyy-mm-dd Thh: mm: ss. mmm.
For example:
Select getdate ()

I sorted out the date format conversion methods that may be frequently used in SQL Server:

Example:

Select CONVERT (varchar, getdate (), 120)
11:06:08
Select replace (CONVERT (varchar, getdate (), 120 ),'-',''),'',''),':','')
20040912110608
Select CONVERT (varchar (12), getdate (), 111)
2004/09/12
Select CONVERT (varchar (12), getdate (), 112)
20040912
Select CONVERT (varchar (12), getdate (), 102)
2004.09.12
Select CONVERT (varchar (12), getdate (), 101)
09/12/2004
Select CONVERT (varchar (12), getdate (), 103)
12/09/2004
Select CONVERT (varchar (12), getdate (), 104)
12.09.2004
Select CONVERT (varchar (12), getdate (), 105)
12-09-2004
Select CONVERT (varchar (12), getdate (), 106)
12 09 2004
Select CONVERT (varchar (12), getdate (), 107)
09 12,200 4
Select CONVERT (varchar (12), getdate (), 108)
11:06:08
Select CONVERT (varchar (12), getdate (), 109)
09 12, 2004 1
Select CONVERT (varchar (12), getdate (), 110)
09-12-2004
Select CONVERT (varchar (12), getdate (), 113)
12 09 2004 1
Select CONVERT (varchar (12), getdate (), 114)
11:06:08. 177
Select getdate ()
Result: 16:52:00. 107
Select convert (char (8), getdate (), 112)
Result: 20031228
Select convert (char (8), getdate (), 108)
Result: 16: 52: 00
Select convert (char (8), getdate (), 112)

Date Format
The rules are as follows:
1 101 us mm/dd/yyyy
2 102 ANSI yy. mm. dd
3 103 UK/French dd/mm/yy
4 104 German dd. mm. yy
5 105 Italian dd-mm-yy
6 106-dd mon yy
7 107-mon dd, yy
8 108-hh: mm: ss
-9 or 109 (*) Default Value + millisecond mon dd yyyy hh: mi: ss: mmmAM (or PM)
10 110 US mm-dd-yy
11 111 yy/mm/dd in Japan
12 112 ISO yymmdd
-13 or 113 (*) European default value + millisecond dd mon yyyy hh: mm: ss: mmm (24 h)
14 114-hh: mi: ss: mmm (24 h)
-20 or 120 (*) ODBC specification yyyy-mm-dd hh: mm: ss [. fff]
-21 or 121 (*) ODBC specifications (in milliseconds) yyyy-mm-dd hh: mm: ss [. fff]
-126 (***) ISO8601 yyyy-mm-dd Thh: mm: ss: mmm (without spaces)
-130 * Kuwait dd mon yyyy hh: mi: ss: mmmAM
-131 * Kuwait dd/mm/yy hh: mi: ss: mmmAM
The format conversion and calculation code between fields in Emaker can be added to "format conversion (read)" and "format conversion (write)" in the attribute )", you can add functions flexibly to the "field" position in table field settings. For example: 'af' + ID or ID + '/' + PWD, convert (varchar (50), F1 ),
Convert (int, %)-19110000 (read)
Convert (char (8), convert (int, %) + 19110000) (write)
Implements the total calculation of rows and so on. Add: % SYSTEM variable % to call the system variable set in the Emaker system.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.