mysql字符串函数

### charset(str) 返回字串字符集
mysql> select charset("张三") from dual;
+-------------------+
| charset("张三")   |
+-------------------+
| utf8              |
+-------------------+
### concat(str,str,...) 连接字串
mysql> select concat("123","ovo") from dual;
+---------------------+
| concat("123","ovo") |
+---------------------+
| 123ovo              |
+---------------------+

### instr(string,substring) 返回substring在string中出现的位置,没有返回0
mysql> select instr(name,"z") from student;
+-----------------+
| instr(name,"z") |
+-----------------+
|               1 |
|               0 |
|               0 |
### ucase(string) 大写
### lcase(string) 小写

### left(string,length) 从string的左边取length个字符
mysql> select left("明天会更好",3) from dual;
+---------------------------+
| left("明天会更好",3)      |
+---------------------------+
| 明天会                    |
+---------------------------+

### length(string) 求出string长度,按照字节求
mysql> select length("你好"),length("hello"),length("123") from dual;
+------------------+-----------------+---------------+
| length("你好")   | length("hello") | length("123") |
+------------------+-----------------+---------------+
|                6 |               5 |             3 |
+------------------+-----------------+---------------+

### replace(替换)
mysql> select replace("manage,hello","manage","world") from dual;
+------------------------------------------+
| replace("manage,hello","manage","world") |
+------------------------------------------+
| world,hello                              |
+------------------------------------------+

### strcmp (比较大小)
mysql> select strcmp("a","b"),strcmp("b","a"),strcmp("a","a") from dual;
+-----------------+-----------------+-----------------+
| strcmp("a","b") | strcmp("b","a") | strcmp("a","a") |
+-----------------+-----------------+-----------------+
|              -1 |               1 |               0 |
+-----------------+-----------------+-----------------+

### substring (取子串),字符下标从1开始
mysql> select substring("abcdefg",2,2) from dual;
+--------------------------+
| substring("abcdefg",2,2) |
+--------------------------+
| bc                       |
+--------------------------+

### ltrim rtrim trim 去掉左右空格
mysql> select ltrim("    zs"),rtrim("zs    "),trim("   zs   ") from dual;
+-----------------+-----------------+------------------+
| ltrim("    zs") | rtrim("zs    ") | trim("   zs   ") |
+-----------------+-----------------+------------------+
| zs              | zs              | zs               |
+-----------------+-----------------+------------------+
posted @ 2023-04-10 19:31  Bre-eZe  阅读(15)  评论(0)    收藏  举报