Home > Database > Mysql Tutorial > body text

How to convert varchar in mysql

藏色散人
Release: 2021-12-16 11:23:58
Original
9884 people have browsed it

Mysql method of converting varchar: 1. Use the cast function to convert; 2. Use the "select -(-field name) from table name;" method to convert; 3. Use "select field name 0 from table name;" "Mode conversion.

How to convert varchar in mysql

The operating environment of this article: windows7 system, mysql5.5 version, Dell G3 computer.

How to convert varchar in mysql?

MySQL varchar type conversion:

Question : Making a data filtering module, due to some problems with the original table design, the fields in the table are strings, and the filtering conditions are numerical values, so there is no way to compare.

Thinking: You can extract the numbers in the mysql field string and then compare them

Solution: Check the data and find three methods

1. Use the cast function

For example: SELECT CAST (field name AS UNSIGNED) FROM table name;

The cast function syntax rule is: Cast (Field name as converted type), where the type can be:

CHAR[(N)] 代表:字符型 
 DATE 代表:日期型
 DATETIME 代表:日期和时间型
 DECIMAL 代表:float型
 SIGNED 代表:int
 TIME 代表:时间型
Copy after login

For a string starting with a number, the result of converting it to a number is to intercept the previous number part, but the beginning part cannot For a string of numbers that is intercepted, the conversion result is 0. This needs to be noted.

2. Use -(-field name)

For example: select -(-field name) from table name;

This is more powerful, Simple and crude

3. Use the field name directly 0,

For example: select field name 0 from table name;

This is only used for field content They are all varchar type numbers and cannot contain Chinese or other characters, otherwise an error will be reported. It is generally recommended to use 1,2.

[Related recommendations: mysql video tutorial]

The above is the detailed content of How to convert varchar in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template