--1 CONVERT (data type, expression), CAST (expression as data type) Transform data type
--Convert a number to a string
SELECT CONVERT (varchar (2), +convert)--+ (varchar (2), ten ) is a connector
--the same
SELECT CAST (as varchar (2)) +cast (as varchar (2))
--Converting a string to an integral type
SELECT Convert (int, ' ten ') +convert (int, '--+ ') is a plus sign
--the same
SELECT CAST (' ten ' as int) +cast (' as int ')
--Convert the date to a character type, with the date format as follows:
without Century (yy) (1) | With
century (yyyy) |
| Standard
Input/output (3) |
- |
0 or 100 (UP) |
Default for datetime and smalldatetime |
Mon dd yyyy hh:miam (or PM) |
1 |
101 |
U.S.. |
1 = mm/dd/yy 101 = mm/dd/yyyy |
2 |
102 |
Ansi |
2 = yy.mm.dd 102 = Yyyy.mm.dd |
3 |
103 |
British/french |
3 = Dd/mm/yy 103 = dd/mm/yyyy |
4 |
104 |
German |
4 = dd.mm.yy 104 = dd.mm.yyyy |
5 |
105 |
Italian |
5 = Dd-mm-yy DD-MM-YYYY = |
6 |
106 (1) |
- |
6 = dd Mon yy 106 = DD Mon yyyy |
7 |
107 (1) |
- |
7 = Mon dd, yy 107 = Mon DD, yyyy |
8 |
108 |
- |
Hh:mi:ss |
- |
9 or 109 (UP) |
Default + milliseconds |
Mon dd yyyy hh:mi:ss:mmmAM (or PM) |
10 |
110 |
USA |
Ten = Mm-dd-yy MM-DD-YYYY = |
11 |
111 |
JAPAN |
one = Yy/mm/dd 111 = Yyyy/mm/dd |
12 |
112 |
Iso |
YYMMDD = YYYYMMDD = |
- |
113 (+) |
Europe default + milliseconds |
DD Mon yyyy hh:mi:ss:mmm (24h) |
14 |
114 |
- |
Hh:mi:ss:mmm (24h) |
- |
or 120 (2) |
ODBC Canonical |
Yyyy-mm-dd Hh:mi:ss (24h) |
- |
or 121 (2) |
ODBC canonical (with milliseconds) default for time, date, datetime2, and DateTimeOffset |
Yyyy-mm-dd hh:mi:ss.mmm (24h) |
- |
126 (4) |
ISO8601 |
Yyyy-mm-ddthh:mi:ss.mmm (no spaces) Note:when the value for milliseconds (MMM) is 0, and the millisecond value is not displayed. For example, the value ' 2012-11-07t18:26:20.000 is displayed as ' 2012-11-07t18:26:20 '. |
- |
127 (6, 7) |
ISO8601 with time zone Z. |
Yyyy-mm-ddthh:mi:ss.mmmz (no spaces) Note:when the value for milliseconds (MMM) is 0, and the milliseconds value is not displayed. For example, the value ' 2012-11-07t18:26:20.000 is displayed as ' 2012-11-07t18:26:20 '. |
- |
130 (+) |
Hijri (5) |
DD Mon yyyy hh:mi:ss:mmmAM In this style, Mon represents a multi-token Hijri Unicode representation of the full month ' s name. This value does not render correctly on a default US installation of SSMS. |
- |
131 (2) |
Hijri (5) |
DD/MM/YYYY Hh:mi:ss:mmmAM |
SELECT CONVERT (varchar), GETDATE (), 126)
SELECT CONVERT (varchar), GETDATE (), 100)
--the same
SELECT CAST (GETDATE () as varchar (10))
--In addition, you can convert to the following format:
--2 Current_User Returns the name of the current user
SELECT Current_User
--3 Datalenfth Returns the number of bytes used to specify an expression
SELECT datalength (' China a ')
SELECT datalength (' 123433 ')
--4 HOST_NAME Returns the name of the computer to which the current user is logged on
SELECT HOST_NAME () --Computer name
SELECT host_id () --Computer ID
--5 System_user Returns the name of the user currently logged in
SELECT System_user
--6 user_name Returns the user name from the given user ID
SELECT user_name (1)
Common system functions in SQL