SQL conversion date field -- CONVERT Conversion Function usage in SQL

Source: Internet
Author: User

Today, I encountered the problem of converting the date field. After the problem was solved with the help of SQL king, I found the relevant materials on the Internet to learn!
SqlDataSource1.SelectCommand = "SELECT ***** '+ RIGHT (XkzSqs. QYMC, LEN (XkzSqs. QYMC)-4) AS requirement enterprise name, XkzSqsSb. SBMC as device name, XkzSqsSb. GGXH as specification model, XkzSqsSb. SL as quantity, CONVERT (nvarchar (10), XkzSqsSb. GZRQ, 126) as date of purchaseFROM XkzSqsSb inner join XkzSqs ON XkzSqsSb. xkzSqsID = XkzSqs. xkzSqsID where (XkzSqsSb. SBMC in ("+ strmc +") and (CHARINDEX ('"+ HidQYMC. value. toString (). trim () + "', XkzSqsSb. SCC)> 0 )";

---------------------------------------- Data learning ------------------------------------------------------------
Format:

CONVERT (data_type, expression [, style])

Note:
This style is generally in the time type (datetime, smalldatetime) and string type (nchar, nvarchar, char, varchar)
It is used only when they are converted to each other.

Example:
Select convert (varchar (30), getdate (), 101) now

Result:
Now
---------------------------------------
| 09/15/2001

========================================================== ======================================

The conversion time of a style number is as follows:

Bytes ------------------------------------------------------------------------------------------------------------
Style (2 bits represent the year) | Style (4 bits represent the year) | input/output format
Bytes ------------------------------------------------------------------------------------------------------------
0 | 100 | mon dd yyyy hh: miAM (or PM)
Bytes ------------------------------------------------------------------------------------------------------------
1 | 101 us | mm/dd/yy
Bytes ------------------------------------------------------------------------------------------------------------
2 | 102 ANSI | yy-mm-dd
Bytes ------------------------------------------------------------------------------------------------------------
3 | 103 English | dd/mm/yy
Bytes ------------------------------------------------------------------------------------------------------------
4 | 104 Germany | dd. mm. yy
Bytes ------------------------------------------------------------------------------------------------------------
5 | 105 Italy | dd-mm-yy
Bytes ------------------------------------------------------------------------------------------------------------
6 | 106 | dd mon yy
Bytes ------------------------------------------------------------------------------------------------------------
7 | 107 | mon dd, yy
Bytes ------------------------------------------------------------------------------------------------------------
Eight | 108 | hh: mm: ss
Bytes ------------------------------------------------------------------------------------------------------------
9 | 109 | mon dd yyyy hh: mi: ss: mmmmAM (or PM)
Bytes ------------------------------------------------------------------------------------------------------------
10 | 110 US | mm-dd-yy
Bytes ------------------------------------------------------------------------------------------------------------
11 | 111 Japan | yy/mm/dd
Bytes ------------------------------------------------------------------------------------------------------------
12 | 112 ISO | yymmdd
Bytes ------------------------------------------------------------------------------------------------------------
13 | 113 European default value | dd mon yyyy hh: mi: ss: mmm (in 24-hour format)
Bytes ------------------------------------------------------------------------------------------------------------
14 | 114 | hh: mi: ss: mmm (in 24-hour format)
Bytes ------------------------------------------------------------------------------------------------------------
20 | 120 ODBC specifications | yyyy-mm-dd hh: mi: ss (24-hour)
Bytes ------------------------------------------------------------------------------------------------------------
21 | 121 | yyyy-mm-dd hh: mi: ss: mmm (in 24-hour format)
Bytes ------------------------------------------------------------------------------------------------------------

Statement and query result:
Select convert (varchar (100), GETDATE (), 0): 05 16 2006 AM
Select convert (varchar (100), GETDATE (), 1): 05/16/06
Select convert (varchar (100), GETDATE (), 2): 06.05.16
Select convert (varchar (100), GETDATE (), 3): 16/05/06
Select convert (varchar (100), GETDATE (), 4): 16.05.06
Select convert (varchar (100), GETDATE (), 5): 16-05-06
Select convert (varchar (100), GETDATE (), 6): 16 05 06
Select convert (varchar (100), GETDATE (), 7): 05 16, 06
Select convert (varchar (100), GETDATE (), 8): 10:57:46
Select convert (varchar (100), GETDATE (), 9): 05 16 2006 10: 57: 46: 827AM
Select convert (varchar (100), GETDATE (), 10): 05-16-06
Select convert (varchar (100), GETDATE (), 11): 06/05/16
Select convert (varchar (100), GETDATE (), 12): 060516
Select convert (varchar (100), GETDATE (), 13): 16 05 2006 10: 57: 46: 937
Select convert (varchar (100), GETDATE (), 14): 10: 57: 46: 967
Select convert (varchar (100), GETDATE (), 20): 10:57:47
Select convert (varchar (100), GETDATE (), 21): 10:57:47. 157
Select convert (varchar (100), GETDATE (), 22): 05/16/06 10:57:47 AM
Select convert (varchar (100), GETDATE (), 23 ):
Select convert (varchar (100), GETDATE (), 24): 10:57:47
Select convert (varchar (100), GETDATE (), 25): 10:57:47. 250
Select convert (varchar (100), GETDATE (), 100): 05 16 2006 AM
Select convert (varchar (100), GETDATE (), 101): 05/16/2006
Select convert (varchar (100), GETDATE (), 102): 2006.05.16
Select convert (varchar (100), GETDATE (), 103): 16/05/2006
Select convert (varchar (100), GETDATE (), 104): 16.05.2006
Select convert (varchar (100), GETDATE (), 105): 16-05-2006
Select convert (varchar (100), GETDATE (), 106): 16 05 2006
Select convert (varchar (100), GETDATE (), 107): 05 16,200 6
Select convert (varchar (100), GETDATE (), 108): 10:57:49
Select convert (varchar (100), GETDATE (), 109): 05 16 2006 10: 57: 49: 437AM
Select convert (varchar (100), GETDATE (), 110): 05-16-2006
Select convert (varchar (100), GETDATE (), 111): 2006/05/16
Select convert (varchar (100), GETDATE (), 112): 20060516
Select convert (varchar (100), GETDATE (), 113): 16 05 2006 10: 57: 49: 513
Select convert (varchar (100), GETDATE (), 114): 10: 57: 49: 547
Select convert (varchar (100), GETDATE (), 120): 10:57:49
Select convert (varchar (100), GETDATE (), 121): 10:57:49. 700
Select convert (varchar (100), GETDATE (), 126): 2006-05-16T10: 57: 49.827
Select convert (varchar (100), GETDATE (), 130): 18 ???? ?????? 1427 10: 57: 49: 907AM
Select convert (varchar (100), GETDATE (), 131): 18/04/1427 10: 57: 49: 920AM

 Note:
Use CONVERT:

CONVERT data_type [ length , expression [ , style ] )

Parameters

Expression

Is any valid Microsoft SQL Server expression ..

Data_type

Data Types provided by the target system, includingBigintAndSQL _variant. User-Defined data types cannot be used.
Length

Nchar,Nvarchar,Char,Varchar,BinaryOrVarbinaryOptional parameter of the data type.

Style

Datetime orSmalldatetimeConvert data to character data (Nchar,Nvarchar,Char,Varchar,NcharOrNvarcharData Type); or string format styleFloat,Real,MoneyOrSmallmoneyConvert data to character data (Nchar,Nvarchar,Char,Varchar,NcharOrNvarcharData Type ).

SQL Server supports the data format in the Arabic style using the Kuwait algorithm.

In the table, the two columns on the left represent datetime orSmalldatetimeConversion to character dataStyleValue. ToStyleValue plus 100 to obtain the four-digit year (yyyy) of the century ).

Without Century digital (yy) Digital Century (yyyy) Standard Input/Output **
- 0 or 100 (*) Default Value Mon dd yyyy hh: miAM (or PM)
1 101 USA Mm/dd/yyyy
2 102 ANSI Yy. mm. dd
3 103 UK/France Dd/mm/yy
4 104 Germany Dd. mm. yy
5 105 Italy 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 USA Mm-dd-yy
11 111 Japan Yy/mm/dd
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 specifications 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 (excluding spaces)
- 130 * Hijri **** Dd mon yyyy hh: mi: ss: mmmAM
- 131 * Hijri **** Dd/mm/yy hh: mi: ss: mmmAM

 

* The default value (style 0, 100, 9, 109, 13, 113, 20, 120, 21, or 121) always returns century digits (yyyy ).
** Input when converted to datetime; output when converted to character data.
* ** It is specially used for XML. ForSmalldatetimeToCharacterData conversion. The output format is shown in the table. ForFloat,MoneyOrSmallmoneyToCharacterData conversion and output are equivalentStyle2. ForRealToCharacterData conversion and output are equivalentStyle1.
* *** Hijri is a calendar system with several variations. Microsoft SQL Server 2000 uses the Kuwait algorithm.

 ImportantBy default, SQL Server interprets two-digit years based on the end year 2049. That is, the year 49 with two digits is interpreted as 2049, and the year 50 with two digits is interpreted as 1950. Many client applications (such as client applications based on OLE automation objects) Use 2030 as the end year. SQL Server provides a configuration option ("") to change the end year used by SQL Server and process the date in a consistent manner. However, the safest way is to specify a four-digit year.

WhenSmalldatetimeWhen converting to character data, the style that contains seconds or milliseconds will display zero at these locations. WhenSmalldatetimeYou can use the appropriateCharOrVarcharThe length of the Data Type to truncate the date part that is not required.

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.