前言:
在MYSQL数据库的使用中,业务数据的增删改查离不开数据治理,当然,这就会涉及到数据库系统内置的一些文本处理函数的使用,来实现数据的规范处理,达到业务应用的目的,涉及到的方法可能包含以下内容:
1、替换:replace(需替换的字段,被替换的字符,替换的内容)
2、截取:substr(需处理的数字和字符串,从1开始的下标序号,截取字符的长度)
3、分割:substring_index(字段,分割符号,M) M指第几个分割符,有正负之分
4、拼接:concat(字段1,字段2,字段3,......)
5、字串检索:locate(待检索的字串,字段,检索起始位置[数字,默认1])
6、小写转换:lower(待转换的字串)
7、大写转换:upper(待转换的字串)
8、长度返回:length(待计算长度的字串)
9、左截取:left(待提取的字符串,指定将从左边返回的字符数)
10、右截取:right(待提取的字符串,指定将从右边返回的字符数)
11、删除指定字串:trim(both| leading | trailing 子串 from 原始字段)
12、清除左空格:ltrim(需要删除左侧空格的字符串)
13、清除右空格:rtrim(需要删除右侧空格的字符串)
14、返回字符串的位置:position(在字符串中搜索的子串 in 要搜索的原始字符串)
模拟数据:
图1-总模拟数据
select id,other_name,replace(other_name,'_','替换内容') from class_score where id = 2
查询结果:
图2-replace
select id,bobby,substr(bobby,1,4) from class_score where id = 2
查询结果:
图3-substr
select id,bobby,substring_index(bobby,',',1) from class_score where id = 2 //若M数字为正,从左往右,查询的是第M分隔符左边所有的内容
查询结果:
图4-substring_index
select id,bobby,substring_index(bobby,',',-1) from class_score where id = 2 ////若M数字为负,从右往左,查询的是第|M|分隔符右边所有的内容
查询结果:
图5-substring_index
select id,name,six,subject,concat(name,'-',six,'-',subject) as desc_ from class_score
查询结果:
图6-concat
select id,name,other_name,locate('a',other_name,2) as desction from class_score where id = 12
从other_name查找a第一次出现的位置。如果a不在other_name中,则返回值为0。
查询结果:
图7-locate
select id,name,other_name,lower(other_name) from class_score where id = 2
查询结果:
图8-lower
select id,name,other_name,upper(other_name) from class_score where id = 1
查询结果:
图9-upper
select id,name,other_name,length(other_name) from class_score where id = 2
查询结果:
图10-length
select id,name,other_name,left(other_name,2) from class_score where id = 12
查询结果:
图11-left
select id,name,other_name,right(other_name,3) from class_score where id = 12
查询结果:
图12-right
SELECT
trim( ' xhx ' ) AS '删除指定字符串中的空格',
trim( LEADING 'x' FROM 'xxhhxx' ) AS '删除指定的首字符',
trim( BOTH 'x' FROM 'xxxhhhxxxhhhxxx' ) AS '删除指定的首尾字符',
trim( TRAILING 'x' FROM 'xxxhhhxxxhhhxxx' ) AS '删除指定的尾字符'
查询结果:
图13-trim
SELECT rtrim( ' xhx ' ) AS '清除右边空格',REPLACE ( rtrim( ' xhx ' ), ' ', '-' ) AS '验证结果'
查询结果:
图14-rtrim
SELECT ltrim( ' xhx ' ) AS '清除左侧空格',REPLACE ( ltrim( ' xhx ' ), ' ', '-' ) AS '验证结果'
查询结果:
图15-ltrim
SELECT POSITION('a' in 'saggxx') as '字符a出现的位置' //若字符串没有所查询的字符则返回0
查询结果:
图16-position
页面更新:2024-06-16
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号