为了账号安全,请及时绑定邮箱和手机立即绑定

MySQL Substring函数

标签:
MySQL

Summary: in this tutorial, we will introduce you to MySQL substring function that allows you to extract a substring out of a string with various options.

The SUBSTRING function returns a substring from a string starting at a specific position with a given length. MySQL provides various forms of the substring function.

MySQL substring function with position

Let’s take a look at a simple form of the SUBSTRING function:

SUBSTR(string,position)

This SUBSTRING  function returns a substring from the string starting from the position. The returned substring starts from the position position to the end of the string.

In the  SUBSTRING string, if you extract a substring from position 1, you get the whole string; if you extract a substring from position 6, you get a substring starting from position 6 to the end of the string.

MySQL substring - string demo

SELECT SUBSTRING('MySQL SUBSTRING',1)

 

MySQL substring function example 1

 

SELECT SUBSTRING('MySQL SUBSTRING',6)

MySQL substring function example 2

The standard SQL syntax of the substring function is as follows:

SUBSTRING(string FROM position)

In this form, the SUBSTRING function uses the FROM keyword, which is more intuitive than the previous one. You can use this form of the SUBSTRING function to rewrite the above examples as the following statements:

SELECT SUBSTRING('MySQL SUBSTRING' FROM 1); SELECT SUBSTRING('MySQL SUBSTRING' FROM 6);

The results are the same as above.

MySQL substring function with position and length

In case you want to specify the length of the substring that you want to extract from a string, you can use the following form of the SUBSTRING function:

SUBSTRING(string,position,length)

This SUBSTRING function returns a substring starting from the position in a given length.In this form of the function, you pass an additional length parameter to the function. See the following example:

SELECT SUBSTRING('MySQL SUBSTRING',1,5)

mysql substring function with position and length

The following is the SQL standard version of the SUBSTRING function, which is longer but more expressive.

SUBSTRING(string FROM position FOR length)

Take a look at the following example:

SELECT SUBSTRING('MySQL SUBSTRING' FROM 1 FOR 5)

 

MySQL substring function with negative position

All the SUBSTRING functions accept not only positive but also negative position. If you use a negative position, the SUBSTRING function will extract the substring from position characters from the end of the string.

MySQLsubstring function with negative position

See the following examples:

SELECT SUBSTRING('MySQL SUBSTRING',-10); SELECT SUBSTRING('MySQL SUBSTRING' FROM -10);

mysql substring function with negative position example

SELECT SUBSTRING('MySQL SUBSTRING',-15,5); SELECT SUBSTRING('MySQL SUBSTRING' FROM -15 FOR 5)

mysql substring function with legnth and negative position

The SUBSTR() is synonym for the  SUBSTRING() so you can use both of them interchangeably.

In this tutorial, we have shown you various forms of the MySQL SUBSTRING function to get a substring starting at a specific position in a given length.

原文链接:http://outofmemory.cn/mysql/function/mysql-substring

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消