1.数据类型转换
方法一:SELECT CAST('123' AS SIGNED);
方法二:SELECT CONVERT('123',SIGNED);
方法三:SELECT '123'+0;
2.计算某字符的个数
length(str1)-length(replace(str,'a',''))
3.判断数值以数字或字母开头
{String} REGEXP '[^0-9.]':如果String中含有不是0-9之间的数字或者是小数点时,返回true ,反之则返回false。
比如说:
select ('123a' REGEXP '[^0-9.]'); --‘123a'中含有字符'a' 输出结果为1 mysql中常量true输出为1 false输出为0
注意:如果字符串有空格,也会返回1,可用trim()函数去掉两端的空格。
(1)筛选不是以数字开头
select * from mot_terms where `name` not REGEXP '^[0-9]'
(2)筛选不是以字母开头
select * from mot_terms where `name` not REGEXP '^[a-zA-Z]'
(3)筛选已数字和特殊字符开头
select * from mot_terms where `name` REGEXP '^[@#$%&0-9]'
4.判断字段是否中文
方法一:
SELECT col FROM table WHERE length(col)!=char_length(col);
当字符集为UTF-8,并且字符为中文时,length() 和 char_length() 两个方法返回的结果是不相同的。
方法二:
SELECT name FROM user WHERE NOT (name REGEXP "[u0391-uFFE5]");