加入收藏 | 设为首页 | 会员中心 | 我要投稿 好传媒网 (https://www.haochuanmei.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

MySQL填充字符串函数

发布时间:2022-10-19 14:01:55 所属栏目:MsSql教程 来源:未知
导读: LPAD(str,len,padstr)
用字符串 padstr对 str进行左边填补直至它的长度达到 len个字符长度,然后返回 str。如果 str的长度长于 len',那么它将被截除到 len个字符。
mysql> SELECT LPA

LPAD(str,len,padstr)

用字符串 padstr对 str进行左边填补直至它的长度达到 len个字符长度,然后返回 str。如果 str的长度长于 len',那么它将被截除到 len个字符。

mysql> SELECT LPAD('hi',4,'??'); -> '??hi'

update account set name = LPAD('京A0',7,Substr(account,length(account)-3,5)) where account like '01300000%' ; ->00883京A

RPAD(str,len,padstr)

用字符串 padstr对 str进行右边填补直至它的长度达到 len个字符长度,然后返回 str。如果 str的长度长于 len',那么它将被截除到 len个字符。

mysql> SELECT RPAD('hi',5,'?ud'); -> 'hi?ud'

update account set name =LPAD('京A0',7,Substr(account,length(account)-3,5)) where account like '01300000%' ; ->京A00883

MySQL 字符串截取函数:left(), right(), substring(), substring_index()。还有 mid(), substr()。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。

1. 字符串截取:left(str, length)

mysql> select left('example.com', 3);

+-------------------------+

| left('example.com', 3) |

+-------------------------+

| exa |

+-------------------------+

2. 字符串截取:right(str, length)

mysql> select right('example.com', 3);

+--------------------------+

| right('example.com', 3) |

+--------------------------+

| com |

+--------------------------+

实例:

#查询某个字段后两位字符

select right(last3, 2) as last2 from historydata limit 10;

#从应该字段取后两位字符更新到另外一个字段

update `historydata` set `last2`=right(last3, 2);

3. 字符串截取:substring(str, pos); substring(str, pos, len)

3.1 从字符串的第 4 个字符位置开始取,直到结束。

mysql> select substring('example.com', 4);

+------------------------------+

| substring('example.com', 4) |

+------------------------------+

| mple.com |

+------------------------------+

3.2 从字符串的第 4 个字符位置开始取Mssq字符串函数,只取 2 个字符。

mysql> select substring('example.com', 4, 2);

+---------------------------------+

| substring('example.com', 4, 2) |

+---------------------------------+

| mp |

+---------------------------------+

3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。

mysql> select substring('example.com', -4);

+-------------------------------+

| substring('example.com', -4) |

+-------------------------------+

| .com |

+-------------------------------+

3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。

mysql> select substring('example.com', -4, 2);

+----------------------------------+

| substring('example.com', -4, 2) |

+----------------------------------+

| .c |

+----------------------------------+

我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。

4. 字符串截取:substring_index(str,delim,count)

4.1 截取第二个 '.' 之前的所有字符。

mysql> select substring_index('#39;, '.', 2);

+------------------------------------------------+

| substring_index('#39;, '.', 2) |

+------------------------------------------------+

| |

+------------------------------------------------+

4.2 截取第二个 '.' (倒数)之后的所有字符。

mysql> select substring_index('#39;, '.', -2);

+-------------------------------------------------+

| substring_index('#39;, '.', -2) |

+-------------------------------------------------+

| example.com |

+-------------------------------------------------+

4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串

mysql> select substring_index('#39;, '.coc', 1);

+---------------------------------------------------+

| substring_index('#39;, '.coc', 1) |

+---------------------------------------------------+

| |

+---------------------------------------------------+

(编辑:好传媒网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!