The use of these three functions in SQL Server Cast,convert,datepart,left,right

Source: Internet
Author: User

These functions in fact, many online data, but in order to facilitate their own memory, I still decided to write a bit, all say good memory than bad writing, so or write a bit more.

The cast () function essentially converts one type to another, such as converting a string type to a time type: SELECT CAST (' 2011-10-2 ' as datetime), or convert the numeric type to a string type: SELECT CAST (1 as BIGINT)

The CONVERT () function is mainly used to display date/time data in different formats: convert (data_type (length),data_to_be_converted,style )

data_type (length) Specifies the target data type (with optional lengths),

The data_to_be_converted contains values that need to be converted,

style Specifies the date/time output format (specific reference: http://www.w3school.com.cn/sql/func_convert.asp)

Example: CONVERT (VARCHAR), GETDATE () results: Dec 11:45 PM

CONVERT (VARCHAR), GETDATE (), 110) Results: 12-29-2008

The DATEPART () function is primarily used to return a separate part of a date/time, such as year, month, day, hour, minute, and so on: DatePart (datepart,date)

The date parameter is a valid day expression. datepart the part to be removed (see also: http://www.w3school.com.cn/sql/func_datepart.asp)

For example: Select DATEPART (YYYY, ' 2011-10-1 ') the part that is removed is: 2011

Left returns the specified number of characters that begin at the beginning of the string. Right returns the specified number of characters starting at the left of the string.

Example: Select Left (' name ', 2); Result: NA

Select Right (' name ', 2) Result: Me

The use of these three functions in SQL Server Cast,convert,datepart,left,right

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.