mysql - varchar type digital conversion and comparison

mysql - varchar type digital conversion and comparison

convert (the object, the target type) is the type commonly used mysql conversion target function. EG: select convert(‘1.123’, decimal(10.4)), the result is 1.1230. Objects can be replaced with a column name.

Two days ago found that when a small partner before a design table to table data type designed as varchar, actually used to store Decimal. I need to filter their data screening

If the content of the article in question, welcome to comment or discuss with me (please specify reasons):

mail: [email protected]
micro-channel: Hello-wgh0807
QQ: 490 536 401

in conclusion

Or used directly convert a digital comparator, when compared with the digital string, the first string of analytical encountered non-numeric type (i.e., letters, etc.) to identify stop, and then successfully resolved portion (left to right , until the exception) into a digital identification. Such as '1.56a8d7' -> 1.56, 'abcdefg...'->0.

If the numeric data is actually determined, it can be compared using digital, such as column>1.1.

If a string is used will need to be converted back to digital or digital storage operations need to be compared, convert recommended method. CONVERT(column, decimal(10,5))The data may be required to modify the actual type.

problem found

I always used to write sql statement to complete all database operations directly in the idea of Database console, this time also, I use the following sql query: select * from test_table where volume > 0. Due to the large amount of data, and did not find any problems, filtered out 0.0000, efficiency is also good.

Test Results 1

But the head of the greatly prefer navicat, after all, visualization, query, modify also convenient. So I downloaded a intuitive to modify the conditions, view the results. But screened to a wrong result (see below):

We can see from the figure, shaix navicat in order volume> 0, '0.00' turned out to be successful by screening! I was skeptical question sql design

analysis test

It should be noted, I used directly to build sql screening is an integer of 0 for the sql statement volume> 0, but when used navicat, due to the entry type is varchar, so that after recognition, become when assembled into the sql volumn > '0', used in the comparison process is more strings, so '0.0'>'0'.

Visual nothing issue, but a guilty conscience I wrote a test sql, for counting:

select count(*) from (SELECT * FROM lh_dc_bond_price_net WHERE dq_volume > 0) a where dq_volume like '0.00%';

Count result is empty, I feel relieved. Suddenly a small partner - Lin question: what will happen if the letters appear? You can correctly identify it? Identify the correct part? Or will complain?

I did an experiment:

select '1.23a>1.23','1.23a'>1.23 UNION select '1.23a<1.23','1.23a'<1.23 UNION select '1.23a=1.23','1.23a'=1.23;

SELECT '1a.23>1','1a.23'> 1 UNION SELECT '1a.23<1','1a.23'< 1 UNION SELECT '1a.23=1','1a.23'=1;

SELECT 'a>0','a'>0 UNION SELECT 'a<0','a'<0 UNION SELECT 'a=0','a'=0;

Three times the results are 0,0,1. Thus can be obtained, when compared with the digital strings, the first string to a parsed, the parsed portions successful (from left to right, until the exception) into a digital identification. Here and convert the same conversion results.

Summary: If you need to compare, you can directly compare the numbers and strings. If the value of the object need conversion, convert function is required to convert or cast.

Data type conversion mysql 5.7 Methods

This part of the main reference mysql reference documentation - 12.10 Cast Functions and Operators

Conventional conversion method: BINARY, CAST, CONVERT.

  • BINARY exprThe object is converted to a binary format, common language and case-sensitive detection spaces.

  • CAST(expr AS type): Receive any type of expression values ​​and generates a result of the specified type, similar features and syntax CONVERT. Is standard SQL syntax.
  • Convert(expr,type)Or Convert(expr using transcoding_name). Any type expression generated result value specified type may be employed.
    • Convert(expr,type)Equivalent to the CAST(expr AS type)function.
    • Convert(expr using transcoding_name)Is standard SQL syntax. Conversion at different coding format

When using convert unused and cast, the type of the value type can include the following

  • BINARY[(N)]

    Generating a data string having BINARY type. About how it affects comparisons, see Section 11.4.2 "BINARY and VARBINARY type." If the optional length N is given, the cast will use no more than the parameter bytes. Byte value is shorter than the length in bytes of padding. BINARY (N) NN0x00N

  • CHAR[(N)] [charset_info]

    Generating a character string having a data type of CHAR. If the optional length N is given, the cause is cast of characters does not exceed the parameters. For less than the value of the character, it will not be filled.

    If not charset_info clause, CHAR generates a default character string set. To explicitly specify the character set, charset_info following values ​​are allowed:

    • CHARACTER SET charset_name: generating a character string of a given set.

    • ASCII: shorthand for CHARACTER SET latin1.

    • UNICODE: shorthand for CHARACTER SET ucs2.

    In all cases, the string has a default collation character set.

  • DATE

    DATE generate value.

  • DATETIME

    Produce DATETIME value.

  • DECIMAL[(M[,D])]

    DECIMAL generate value. If the optional values ​​of M and D values, the maximum number of bits that specify the number of bits (precision) and after the decimal point (scale).

  • JSON (added in MySQL 5.7.8)

    Generating JSON value. For more information about the rules of conversion between JSON values ​​of other types, see the comparison and ordering JSON value.

  • NCHAR[(N)]

    Similarly CHAR, but produces a string with a national character set. See Section 10.3.7, "National Character Set" .

    And different CHAR, NCHAR character set information are not allowed to specify trailing.

  • SIGNED [INTEGER]

    Generating a signed integer value.

  • TIME

    TIME generate value.

  • UNSIGNED [INTEGER]

    Generating unsigned integer value.

For more information see mysql Reference Manual

Reference documents:

MySQL string comparison of the digital pit - Live In A Dream

mysql reference documentation - 12.3.2 Comparison Functions and Operators

[Mysql reference documentation - 12.10 Cast Functions and Operators] (

Guess you like

Origin www.cnblogs.com/wgh0807/p/11432320.html