mysql 字符串操作函数

8、字符串操作

这里目录标题

    • 8、字符串操作
      • 8.0、字符串操作函数
        • 8.0.1. concat()与concat_ws()
        • 8.0.2. group concat()
        • 8.0.3. lenght()与char_length()
        • 8.0.4. 截取字符串 substring()
        • 8.0.5. replace() 替换字符串
        • 8.0.6. ifnull()
        • 8.0.7. coalesce()
        • 8.0.8. strcmp()
      • 8.1、转大小写
      • 8.2、首字母大写

8.0、字符串操作函数

8.0.1. concat()与concat_ws()

  • concat(str1,str2…): 将多个字符串连接成一个字符串
  • concat_ws(‘连接符’,str1,str2): 指定分隔符进行拼接字符串
concat(str1, str2,...) 
concat(str1, seperator,str2,seperator,...)
concat_ws(separator, str1, str2, ...)
select concat("123","456","789") "concat",concat_ws("-","123","456") "concatws"
-- 查询结果
|concat    |concatws |
+----------+---------+
|123456789 |123-456  |
+----------+---------+

8.0.2. group concat()

  • group concat() 将group by产生的同一个分组中的值连接起来,返回一个字符串结果
GROUP_CONCAT([DISTINCT] expr [,expr ...] [order by 排序字段 asc/desc  ] [separator '分隔符'])
select
	deptno,
	GROUP_CONCAT(distinct EMPNO order by EMPNO desc separator ';') "group-concat"
from
	emp
group by
	deptno;

-- 运行结果 
|deptno|group-concat                 |
+------+-----------------------------+
|    10|7934;7839;7782               |
|    20|7902;7876;7788;7566;7369     |
|    30|7900;7844;7698;7654;7521;7499|
|    40|8000                         |

8.0.3. lenght()与char_length()

  • lenght(str):判定字符串长度,返回字符串所占的字节数
  • char_length(str):返回字符串的字符数
length("String")
char_length("String")
select length("字符串有多长") "len",char_length("字符串有多长") "c-l"
-- 查询结果
|len|c-l|
+---+---+
| 18|  6|

8.0.4. 截取字符串 substring()

  • substring(str,a,b):提取字段中的一段,从字符串str的第a位开始提取,提取b个字符

  • left(str,n):提取字符串最左边的n个字符

  • right(str,n):提取字符串最右边的n个字符(该例未用到)

select substring("字符串有多长",4,3) "len"
-- 结果
len  |
------+
有多长|
select left("字符串有多长",3) "left"
--  结果 
left  |
------+
字符串 |
select right("字符串有多长",3) "right"
-- 结果
right  |
-------+
有多长  |

8.0.5. replace() 替换字符串

  • replace(str,old_string,new_string); 替换字符串
  • 以上語法的意思是,在字串 str1 中,當 str2 出現時,將其以 str3 替代。
SELECT REPLACE (Region_Name, 'ast', 'astern') FROM Geography;

select replace("字符串有多长","字符串","String") "replace"
-- 结果
     replace|
------------+
String有多长 |

8.0.6. ifnull()

  • ifnull(expression, alt_value) 判断是否为Null
    • 如果第一个参数的表达式 expression 为 NULL,则返回第二个参数的备用值。
select ifnull(comm,1) from emp;
-- ------  结果
|ifnull(comm,11)|
+---------------+
|         1400.0|
|            0.0|
|           11.0|
|           11.0|

8.0.7. coalesce()

  • coalesce(str1,str2…); 同等于 ifnull
    • 从值列表中返回第一个非NULL的值,当遇到NULL值时将其替换为0。
select coalesce(comm,1) from emp;
-- -------结果
|coalesce(comm,1)|
+----------------+
|          1400.0|
|             0.0|
|             1.0|
|             1.0|

select coalesce(null,null,9,1,0)
-- --------------------------
|coalesce(null,null,9,1,0)|
+-------------------------+
|                        9|

8.0.8. strcmp()

  • STRCMP(string1, string2) 比较的两个字符串
    • 相等返回0,小于返回-1,大于返回1
SELECT STRCMP("SQL Tutorial", "SQL Tutorial"); 
select strcmp(1,0),strcmp(0,1),strcmp(1,1)
-- 结果------------------------
strcmp(1,0)|strcmp(0,1)|strcmp(1,1)|
-----------+-----------+-----------+
          1|         -1|          0|

8.1、转大小写

  • lower(’字符串‘):转小写
  • uppper(’字符串‘) :转大写
select lower("ARAY"),upper("all")
-- 结果------------------
lower("ARAY")|upper("all")|
-------------+------------+
aray         |ALL         |

8.2、首字母大写

CONCAT(UPPER(left(ename,1)),LOWER(RIGHT(ename,length(t_ename)-1))) as name
select
	CONCAT(UPPER(left(ename, 1)), LOWER(right(ename, length(ename)-1))) as name
from
	emp e
-- jieguo --------------------
|name  |
+------+
|Smith |
|Allen |
|Ward  |
|Jones |
|Martin|

你可能感兴趣的:(SQL,msyql函数,字符串函数,mysql)