Summary of usage of SQL convert conversion function

Source: Internet
Author: User
Tags format copy getdate sql
format:
CONVERT (Data_type,expression[,style])

Description:
This style is generally in the type of time (datetime,smalldatetime) and String type (Nchar,nvarchar,char,varchar)
When they are converted to each other.

Example:
SELECT CONVERT (varchar), GETDATE (), () now
The results are:
Now
---------------------------------------
09/15/2001

The style number has the following meanings when converting time:

Copy CodeThe code is as follows:
Style (2-bit year) style (4-bit year) Input output format

0 Mon dd yyyy hh:miam (or PM)
1 101 American Mm/dd/yy
2 102 ANSI YY-MM-DD
3 103 English-French Dd/mm/yy
4 104 German Dd.mm.yy
5 105 Italian Dd-mm-yy
6 Mon dd yy
7 Mon Dd,yy
8 108 Hh:mm:ss
9 109 Mon dd yyyy Hh:mi:ss:mmmmAM (or PM)
10 110 American Mm-dd-yy
11 111 Japanese Yy/mm/dd
112 ISO YYMMDD
13 113 Euro Default DD Mon yyyy hh:mi:ss:mmm (24-hour system)
114 Hh:mi:ss:mmm (24-hour system)

ODBC Specification Yyyy-mm-dd Hh:mi:ss (24-hour system)

121 Yyyy-mm-dd Hh:mi:ss:mmm (24-hour system)


statement and Query results:
Copy CodeThe code is as follows:
SELECT CONVERT (varchar), GETDATE (), 0): 2006 10:57am
SELECT CONVERT (varchar), GETDATE (), 1): 05/16/06
SELECT CONVERT (varchar), GETDATE (), 2): 06.05.16
SELECT CONVERT (varchar), GETDATE (), 3): 16/05/06
SELECT CONVERT (varchar), GETDATE (), 4): 16.05.06
SELECT CONVERT (varchar), GETDATE (), 5): 16-05-06
SELECT CONVERT (varchar), GETDATE (), 6): 16 05 06
SELECT CONVERT (varchar), GETDATE (), 7:05 16, 06
SELECT CONVERT (varchar), GETDATE (), 8): 10:57:46
SELECT CONVERT (varchar), GETDATE (), 9): 2006 10:57:46:827am
SELECT CONVERT (varchar), GETDATE (), 10): 05-16-06
SELECT CONVERT (varchar), GETDATE (), 11): 06/05/16
SELECT CONVERT (varchar), GETDATE (), 12): 060516
SELECT CONVERT (varchar), GETDATE (), 13:16 05 2006 10:57:46:937
SELECT CONVERT (varchar), GETDATE (), 14): 10:57:46:967
SELECT CONVERT (varchar), GETDATE (), 20): 2006-05-16 10:57:47
SELECT CONVERT (varchar), GETDATE (), 21): 2006-05-16 10:57:47.157
SELECT CONVERT (varchar (MB), GETDATE (): 05/16/06 10:57:47 AM
SELECT CONVERT (varchar), GETDATE (), 23): 2006-05-16
SELECT CONVERT (varchar), GETDATE (), 24): 10:57:47
SELECT CONVERT (varchar), GETDATE (), 25): 2006-05-16 10:57:47.250
SELECT CONVERT (varchar), GETDATE (): 2006 10:57am
SELECT CONVERT (varchar), GETDATE (), 101): 05/16/2006
SELECT CONVERT (varchar), GETDATE (), 102): 2006.05.16
SELECT CONVERT (varchar), GETDATE (), 103): 16/05/2006
SELECT CONVERT (varchar), GETDATE (), 104): 16.05.2006
SELECT CONVERT (varchar), GETDATE (), 105): 16-05-2006
SELECT CONVERT (varchar), GETDATE (), 106): 16 05 2006
SELECT CONVERT (varchar), GETDATE (), 107:05 16, 2006
SELECT CONVERT (varchar), GETDATE (), 108): 10:57:49
SELECT CONVERT (varchar), GETDATE (), 109): 2006 10:57:49:437am
SELECT CONVERT (varchar), GETDATE (), 110): 05-16-2006
SELECT CONVERT (varchar), GETDATE (), 111): 2006/05/16
SELECT CONVERT (varchar), GETDATE (), 112): 20060516
SELECT CONVERT (varchar), GETDATE (), 113:16 05 2006 10:57:49:513
SELECT CONVERT (varchar), GETDATE (), 114): 10:57:49:547
SELECT CONVERT (varchar), GETDATE (), 120): 2006-05-16 10:57:49
SELECT CONVERT (varchar), GETDATE (), 121): 2006

the CONVERT () function commonly used by SQL

Time to use, the second time to check, the time to forget, simply write down.
Copy CodeThe code is as follows:
SELECT CONVERT (varchar), GETDATE (), 0): 2006 10:57am
SELECT CONVERT (varchar), GETDATE (), 1): 05/16/06
SELECT CONVERT (varchar), GETDATE (), 2): 06.05.16
SELECT CONVERT (varchar), GETDATE (), 3): 16/05/06
SELECT CONVERT (varchar), GETDATE (), 4): 16.05.06
SELECT CONVERT (varchar), GETDATE (), 5): 16-05-06
SELECT CONVERT (varchar), GETDATE (), 6): 16 05 06
SELECT CONVERT (varchar), GETDATE (), 7:05 16, 06
SELECT CONVERT (varchar), GETDATE (), 8): 10:57:46
SELECT CONVERT (varchar), GETDATE (), 9): 2006 10:57:46:827am
SELECT CONVERT (varchar), GETDATE (), 10): 05-16-06
SELECT CONVERT (varchar), GETDATE (), 11): 06/05/16
SELECT CONVERT (varchar), GETDATE (), 12): 060516
SELECT CONVERT (varchar), GETDATE (), 13:16 05 2006 10:57:46:937
SELECT CONVERT (varchar), GETDATE (), 14): 10:57:46:967
SELECT CONVERT (varchar), GETDATE (), 20): 2006-05-16 10:57:47
SELECT CONVERT (varchar), GETDATE (), 21): 2006-05-16 10:57:47.157
SELECT CONVERT (varchar (MB), GETDATE (): 05/16/06 10:57:47 AM
SELECT CONVERT (varchar), GETDATE (), 23): 2006-05-16
SELECT CONVERT (varchar), GETDATE (), 24): 10:57:47
SELECT CONVERT (varchar), GETDATE (), 25): 2006-05-16 10:57:47.250
SELECT CONVERT (varchar), GETDATE (): 2006 10:57am
SELECT CONVERT (varchar), GETDATE (), 101): 05/16/2006
SELECT CONVERT (varchar), GETDATE (), 102): 2006.05.16
SELECT CONVERT (varchar), GETDATE (), 103): 16/05/2006
SELECT CONVERT (varchar), GETDATE (), 104): 16.05.2006
SELECT CONVERT (varchar), GETDATE (), 105): 16-05-2006
SELECT CONVERT (varchar), GETDATE (), 106): 16 05 2006
SELECT CONVERT (varchar), GETDATE (), 107:05 16, 2006
SELECT CONVERT (varchar), GETDATE (), 108): 10:57:49
SELECT CONVERT (varchar), GETDATE (), 109): 2006 10:57:49:437am
SELECT CONVERT (varchar), GETDATE (), 110): 05-16-2006
SELECT CONVERT (varchar), GETDATE (), 111): 2006/05/16
SELECT CONVERT (varchar), GETDATE (), 112): 20060516
SELECT CONVERT (varchar), GETDATE (), 113:16 05 2006 10:57:49:513
SELECT CONVERT (varchar), GETDATE (), 114): 10:57:49:547
SELECT CONVERT (varchar), GETDATE (), 120): 2006-05-16 10:57:49
SELECT CONVERT (varchar), GETDATE (), 121): 2006-05-16 10:57:49.700
SELECT CONVERT (varchar), GETDATE (), 126): 2006-05-16t10:57:49.827
SELECT CONVERT (varchar), GETDATE (), 130):???? ?????? 1427 10:57:49:907am
SELECT CONVERT (varchar), GETDATE (), 131): 18/04/1427 10:57:49:920am


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.