数据库常用函数(字符函数)

对表中的字符型字段可以进行操作

一、计算字符的长度
            格式:length('string')
            使用:select length('string') from 表名1
二、全部转化为小写
            格式:lower('string')
            使用:select lower('string') from 表名1
三、全部转化为大写
            格式:upper('string')
            使用:select upper('string') from 表名1
四、截取字符串
            格式1:substr('string',start,length)
            start代表开始位置,length代表截取长度。
            格式2:substring_index('string','符号','次数')
            格式2针对的是字符串中有符号的情况,“符号”位填入的是碰到之后要停止的符号,“次数”位是填入第几次碰到这个符号后停止                                 截取。
            example:
            select substring_index('hello:world:woshi:shui',':',2)
            此时截取到的字符串为:hello:world
            使用:select substr()     from 表1
五、从指定左侧读取字符串
            格式:left('string',length)
            使用:select left('string') from 表1
六、从指定右读取字符串
            格式:right('string',length)
七、去掉左侧的空格
            ltrim('      string')
八、去掉右侧的空格
            rtrim('string     ')
九、去掉两侧的空格或者指定字符
            trim(both|leading|trailing'set'from'string')
            解释:both是trim默认的参数,为去掉两边的空格;
                       leading是去掉左侧的指定字符,需要和from搭配
                       trailing是去掉右侧的指定字符,需要和from搭配
                        set为指定字符
            example:select trim('     shangbonihao       ')
                              select trim(leading'sh'from'shangbonihao')
十、给左侧添加空格
            格式:lpad('string','padstring',length)
            使用:select lpad('string','padstring',length)
十一、给右侧添加空格
            格式:rpad('string',length,'padstring')
            使用:select lpad('string',length,'padstring')
十二、取得指定子串在字符串中的位置
            格式1:instr('string','子串')
            格式2:position('string','子串')
            格式3:locate('string','子串',start)
            start为开始位置
            使用:select instr('子串','string')
十三、颠倒字符串
            格式:reverse('string')
            使用:select reverse('string')
十四、替换指定字符串
            格式:replace('srting','子串','替换')
            使用:select replace.........
十五、拼接字符串
            格式:concat('sting1','string2')
            使用:select concat('string1','string2')
十六、取得字符的ASCII码
            格式:ascii('string')
            使用:select ascii('string')
十七、将ascii转化为字符
            格式:char('ascii码')
            使用:select char('ascii')
十八、匹配发音
            格式:soundex('string')
            使用:select soundex('string')
十九、将制定字符串重复值得次数
            格式:repeat('string',count)
            count为重复次数
            使用:select('string',count)
原文地址:https://www.cnblogs.com/SoulReaper/p/3309780.html