Type conversions for SQL

Source: Internet
Author: User

When it comes to SQL type conversions, we know that SQL is a weakly typed language, so you can do an implicit coercion type conversion, and the following is a record of the type conversion problems encountered in these days.

1. In sql, conversion between string types and numbers does not require coercion of type conversions, and if the string is a numeric string, it is converted directly to a numeric type, otherwise an error will be put.

2. In SQL, if a field data type is a character type, you must add ' (single quotation marks), unless the numeric value is numeric.

3. In SQL, there is an interesting phenomenon if datetime type data is not enclosed in single quotation marks, the system will give a default time.

There are also two types of conversion functions in SQL:

1.CAST (expression as data_type)

2.CONVERT (Data_type, Expression,[style])

SQL if the + number on both sides of the number and string type of two data, then the numeric addition processing, if the string type can not be forced to convert to a number, then an error.

Select 1+'a'

This code fragment will have an error that should be correct as follows:

Select ' a '+cast (1asChar(1))Select'A  '+convert (char(1),1)

Type conversions for SQL

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.