length()与trim()函数用法

image

student表

SELECT  *  from  `student` where length(sex) = 0

 

image

SELECT  length(ID)   from  `student`  WHERE  province ='浙江'

 

image

 

length针对字符,而lengthb是针对字节的。

字符与字节的关系,如1个英文字母占一个字符,中文占两个字符。

image

image

该软件简化了length与lengthb的用法?

 

Trim() 函数  

功能就是使用它来去除字符串的行首和行尾的空格,这个功能也是大家使用频率最高的一种。

image

 

select trim(province) FROM `student` where ID IN (23,24)

 

image

 

1. trim()删除字符串两边的空格。
2. ltrim()删除字符串左边的空格。
3. rtrim()删除字符串右边的空格。
4. trim('字符1' from '字符串2') 分别从字符2串的两边开始,删除指定的字符1。
5. trim([leading | trailing | both] trim_char from string) 从字符串String中删除指定的字符trim_char。
    leading:从字符串的头开始删除。
    trailing:从字符串的尾部开始删除。
    borth:从字符串的两边删除。
6. tim()只能删除半角空格。

例如:

select *,trim('江'from'江苏') FROM `student` where ID IN (23,24)

 

image

 

 

image

 

select *,trim(LEADING  'X' from 'XXXX江都' )  FROM `student` where ID = 24;

select *,trim(BOTH  'X' from '江XXXXX苏' )  FROM `student` where ID =23;

select *,trim(trailing 'X' from '安徽XXXXXX' )  FROM `student` where ID =22;

 

image

image

image

 

******trim()函数与length()函数混用,例如:

image

posted @ 2017-02-07 12:26  风和日丽2010  阅读(3848)  评论(0编辑  收藏  举报