Provides you with an in-depth understanding of the MYSQL Cast function.

Source: Internet
Author: User

The MYSQL Cast function is a very important MYSQL function. The following describes the syntax and usage of the MYSQL Cast function in detail, hoping that you can have a better understanding of the MYSQL Cast function.

BINARY
The BINARY operator returns a BINARY string. This is a simple way to promote column comparison by byte rather than by character. This makes the comparison case sensitive, even if the column is not defined as BINARY or BLOB. BINARY will also generate trailing white space, making it more conspicuous.

 
 
  1. mysql>   SELECT   'a'   =   'A';     
  2.       
  3.                   ->   1     
  4.       
  5. mysql>   SELECT   BINARY   'a'   =   'A';     
  6.                    ->   0     
  7. mysql>   SELECT   'a'   =   'a   ';     
  8.                    ->   1     
  9. mysql>   SELECT   BINARY   'a'   =   'a   ';     
  10.                    ->   0    

BINARY affects the entire comparison; it can be given before any operand and produce the same result.

BINARY str is the scaling form of CAST (str as binary.

Note: In some contexts, if you send an indexed column to BINARY, MySQL will not be able to use this index effectively.
If you want to compare the case sensitivity of a BLOB value or other binary strings, you can use the fact that the binary string does not have a character set to achieve this purpose, so that there is no concept of a folder. To perform a case-sensitive comparison, you can use the CONVERT () function to CONVERT a string value to a case-insensitive character set. The result is a non-binary string, so the LIKE operation is not case sensitive:

 
 
  1. SELECT   'A'   LIKE   CONVERT(blob_col   USING   latin1)   FROM   tbl_name;     

To use a different character set, replace latin1 in the preceding statement.

CONVERT () is generally used to compare strings that appear in different character sets.
CAST (expr AS type), CONVERT (expr, type), CONVERT (expr USING transcoding_name)
CAST () and CONVERT () functions can be used to obtain values of one type and generate values of another type.

This type can be one of the following values:
BINARY [(N)]
CHAR [(N)]
DATE
DATETIME
DECIMAL
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
BINARY generates a BINARY string.

If a random length of N is given, BINARY [N] causes cast to use this parameter no more than N Bytes. Similarly, CHAR [N] causes cast to use this parameter for no more than N characters.

 
 
  1. CAST () and CONVERT (... USING...) is a standard SQL syntax. The non-USING format of CONVERT () is the ofis ODBC syntax.

CONVERT () with USING is used to CONVERT data between different character sets. In MySQL, the automatic decoding name is the same as the corresponding Character Set Name. For example. This statement converts the 'abc' character set in the server's default character set to the corresponding character string in the utf8 character set:

 
 
  1. SELECT   CONVERT('abc'   USING   utf8);    

The cast function is useful when you want to CREATE a special type column in a CREATE... SELECT statement:

 
 
  1. CREATE   TABLE   new_table   SELECT   CAST('2000-01-01'   AS   DATE);    

This function is also used to sort ENUM columns in the lexical order. Generally, sorting of ENUM columns occurs when internal values are used. Send these values to the CHAR result in the lexical order:

 
 
  1. SELECT   enum_col   FROM   tbl_name   ORDER   BY   CAST(enum_col   AS   CHAR);    


CAST (str as binary) is the same as binary str. CAST (expr as char) treats the expression AS a string with the default character set.

If a part of a complex expression such as concat ('date: ', CAST (NOW () AS Date) is used, CAST () also changes the result.

You should not use CAST () to EXTRACT data in different formats, but you can use string functions such as LEFT () or EXTRACT () instead.

To assign a string to a value in a numeric context, you do not need to do anything except to use the string value as a number:

 
 
  1. mysql>   SELECT   1+'1';     
  2.                 ->   2    

To use a number in a string context, the number is automatically converted to a BINARY string.

 
 
  1. mysql>   SELECT   CONCAT('hello   you   ',2);     
  2.                   ->   'hello   you   2'    

MySQL supports the operation of signed and unsigned 64-bit values. If you are using a numeric operator (such as +) and one of the operands is an unsigned integer, the result is unsigned. You can use the SIGNED and UNSIGNED cast operators to overwrite it. Assign the operation to a signed or unsigned 64-bit integer.

 
 
  1. mysql>   SELECT   CAST(1-2   AS   UNSIGNED)     
  2.                   ->   18446744073709551615     
  3. mysql>   SELECT   CAST(CAST(1-2   AS   UNSIGNED)   AS   SIGNED);     
  4.                   ->   -1    

Note that if any operand is a floating point value, the result is a floating point value and is not affected by the above rules (for this purpose, the DECIMAL column value is considered as a floating point value ).

 
 
  1. mysql>   SELECT   CAST(1   AS   UNSIGNED)   -   2.0;     
  2.                   ->   -1.0     
  3.      

If you use a string in an arithmetic operation, it is converted into a floating point number.

Learn more about mysql CONCAT () Functions

How to view the three MySQL character sets

View MySQL Default Character Set

How MySQL defines foreign keys

MySql stored procedure with Parameters

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.