SQL Server 2000 functions use---CAST and CONVERT

Source: Internet
Author: User

This article from: http://www.cnblogs.com/xh831213/category/47654.html

Explicitly converts an expression of a data type to another data type. CAST and CONVERT provide similar functionality.

Grammar

Using CAST:

CAST (expression as data_type)

Using CONVERT:

CONVERT (data_type[(length)], expression [, style])

Parameters

Expression

Is any valid microsoft®sql Server™ expression.

Data_type

The data types provided by the target system, including bigint and sql_variant. User-defined data types cannot be used

Length

Optional parameters for nchar, nvarchar, char, varchar, binary, or varbinary data types.

Style

A date format style whereby DateTime or smalldatetime data is converted to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data type) or a string format style that will float, Real, money, or smallmoney data is converted to character data (nchar, nvarchar, char, varchar, nchar, or nvarchar data types).

SQL Server supports data formats in Arabian styles using the Kuwaiti algorithm.

In the table, the two columns on the left represent the style values that convert datetime or smalldatetime to character data. Add 100 to the style value to get a four-bit year (yyyy) that includes century digits.

without century digits (yy) with century digits (yyyy)
Standard

Input/Output * *
- 0 or 100 (*) Default value Mon dd yyyy hh:miam (or PM)
1 101 United States 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 + milliseconds Mon dd yyyy hh:mi:ss:mmmAM (or PM)
10 110 United States Mm-dd-yy
11 111 Japan Yy/mm/dd
12 112 Iso Yymmdd
- 13 or 113 (*) European default + milliseconds DD Mon yyyy hh:mm:ss:mmm (24h)
14 114 - Hh:mi:ss:mmm (24h)
- 20 or 120 (*) ODBC specification YYYY-MM-DD HH:MM:SS[.FFF]
- 21 or 121 (*) ODBC specification (with milliseconds) YYYY-MM-DD HH:MM:SS[.FFF]
- 126 (* *) ISO8601 YYYY-MM-DD Thh:mm:ss:mmm (no spaces included)
- 130* Kuwait DD Mon yyyy hh:mi:ss:mmmAM
- 131* Kuwait Dd/mm/yy Hh:mi:ss:mmmAM

The

*     default value (style 0 or 100, 9 or 109, 13 or 113, 20, or 120, 21, or 121) always returns century digits (yyyy).
* * input when converting to datetime, and output when converting to character data.
* * * is dedicated to XML. For conversions from DateTime or smalldatetime to character data, the output format is shown in the table. For conversions from float, money, or smallmoney to character data, the output is equivalent to style 2. For conversions from Real to character data, the output is equivalent to style 1.
Example: CONVERT (char (Ten), S.season_plandate, 102) as scheduled date

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.