MySQL截取字符串函數(shù)方法
MySQL截取字符串函數(shù)是MySQL中功能強大的函數(shù),下面就為您詳細(xì)介紹MySQL截取字符串函數(shù)的用法,希望對您能夠有所幫助。
今天建視圖時,用到了MySQL中的字符串截取,很是方便。
感覺上MySQL的字符串函數(shù)截取字符,比用程序截?。ㄈ鏟HP或JAVA)來得強大,所以在這里做一個記錄,希望對大家有用。
MySQL截取字符串函數(shù):
1、從左開始截取字符串
left(str, length)
說明:left(被截取字段,截取長度)
例:select left(content,200) as abstract from my_content_t
2、從右開始截取字符串
right(str, length)
說明:right(被截取字段,截取長度)
例:select right(content,200) as abstract from my_content_t
3、截取字符串
substring(str, pos)
substring(str, pos, length)
說明:substring(被截取字段,從第幾位開始截取)
substring(被截取字段,從第幾位開始截取,截取長度)
例:select substring(content,5) as abstract from my_content_t
select substring(content,5,200) as abstract from my_content_t
(注:如果位數(shù)是負(fù)數(shù) 如-5 則是從后倒數(shù)位數(shù),到字符串結(jié)束或截取的長度)
4、按關(guān)鍵字截取字符串
substring_index(str,delim,count)
說明:substring_index(被截取字段,關(guān)鍵字,關(guān)鍵字出現(xiàn)的次數(shù))
例:select substring_index(”blog.chinabyte.com”,”.”,2) as abstract from my_content_t
結(jié)果:blog.chinabyte
(注:如果關(guān)鍵字出現(xiàn)的次數(shù)是負(fù)數(shù) 如-2 則是從后倒數(shù),到字符串結(jié)束)
結(jié)果:chinabyte.com
如果關(guān)鍵字在字符串找不到,則返回字符串全部內(nèi)容
【編輯推薦】