能运行在多个系统上的代码具有可移植性,在数据库软件中,多数sql语句时可移植的,可移植性比较强;而函数的移植性不强,主要是由于各种数据库软件都支持自己所特有的函数。因此许多sql用户不认同使用数据库软件所特有的的函数
字符串函数
concat(str1,...,strn) : 连接n个字符串为一个完整的字符串
insert(str,x,y,instr) : 将字符串str从第x位置开始,y个字符长的子串替换为instr
lower(str) : 所有字符变为小写
upper(str) :所有字符变为大写
left(str) : 返回字符串最左边的x个字符
right(str) : 返回字符串str最右边的x个字符
lpad(str,n,pad) : 使用pad字符串对字符串最左边进行填充,直到长度为n个字符
rpad(str,n,pad) : 使用pad字符串对字符串最右边进行填充,直到长度为n个字符
ltrim(str) : 去掉字符串str最左边的空格
rtrim(str) : 去掉字符串str最右边的空格
repeat(str,x) : 返回字符串str重复x次的结果
replace(str,a,b) : 使用字符串b替换字符串str中所有出现的字符串
strcmp(str1,str2) : 比较字符串str1和str2
trim(str) : 去掉字符串str行头和行尾的空格
substring(str,x,y) : 返回字符串str中从x位置起y个字符串长度的字符串
1、concat(str1,...,strn)
mysql> select concat('M','y','sql');
+-----------------------+
| concat('M','y','sql') |
+-----------------------+
| Mysql |
+-----------------------+
mysql> select concat('M','y','sql',null);
+----------------------------+
| concat('M','y','sql',null) |
+----------------------------+
| NULL |
+----------------------------+
注意:如果传入的参数有null,则返回的结果将是null
2、insert(str,x,y,instr)
mysql> select insert('mysql learning',3,5,'miss you');
+-----------------------------------------+
| insert('mysql learning',3,5,'miss you') |
+-----------------------------------------+
| mymiss youearning |
+-----------------------------------------+
3、strcmp(str1,str2)
mysql> select strcmp('my','sql');
+--------------------+
| strcmp('my','sql') |
+--------------------+
| -1 |
+--------------------+
1 row in set (0.01 sec)
mysql> select strcmp('mysd','mysql');
+------------------------+
| strcmp('mysd','mysql') |
+------------------------+
| -1 |
+------------------------+
1 row in set (0.00 sec)
mysql> select strcmp('mysd','mysal');
+------------------------+
| strcmp('mysd','mysal') |
+------------------------+
| 1 |
+------------------------+
1 row in set (0.00 sec)
mysql> select strcmp('mysd','mysd');
+-----------------------+
| strcmp('mysd','mysd') |
+-----------------------+
| 0 |
+-----------------------+
4、获取字符串长度length()和字符数函数char_length()
mysql> select 'mysql' as '英文字符串字节长度',
-> length('mysql') as '字符串长度',
-> '常建功' as '中文字符串',
-> length('常建功') as '字符串字节长度';
+--------------------+------------+------------+----------------+
| 英文字符串字节长度 | 字符串长度 | 中文字符串 | 字符串字节长度 |
+--------------------+------------+------------+----------------+
| mysql | 5 | 常建功 | 6 |
+--------------------+------------+------------+----------------+
select 'mysql' as '英文字符串',char_length('mysql') as '字符串字符数', '常建功' as '中文字符串',char_length('常建功') as '字符串字符数';
+------------+--------------+------------+--------------+
| 英文字符串 | 字符串字符数 | 中文字符串 | 字符串字符数 |
+------------+--------------+------------+--------------+
| mysql | 5 | 常建功 | 6 |
+------------+--------------+------------+--------------+
理论上是3,但是实际上我的显示时6.
5、大小字母转换upper()和lower()
select upper('aBcD') as 'aBcD',lower('HJjdIUE') as 'HJjdIUE';
+------+---------+
| aBcD | HJjdIUE |
+------+---------+
| ABCD | hjjdiue |
+------+---------+
6、查找字符串
find_in_set(),field(),locate(),position(),instr(),ELT()
find_in_set(str1,str2)获取相匹配字符串的位置,参数str2中将包含若干个用逗号隔开的字符串
mysql> select find_in_set('mysql','I,love,mysql,and,you?') as '位置';
+------+
| 位置 |
+------+