牛骨文教育服务平台(让学习变的简单)
博文笔记

MySQL 字符串截取函数

创建时间:2016-10-03 投稿人: 浏览次数:28190
  1. MySQL 字符串截取函数:left(), right(), substring(), substring_index()。还有 mid(), substr()。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。  
  2. 1. 字符串截取:left(str, length)  
  3. mysql> select left("sqlstudy.com", 3);  
  4. +-------------------------+  
  5. | left("sqlstudy.com", 3) |  
  6. +-------------------------+  
  7. | sql                     |  
  8. +-------------------------+  
  9. 2. 字符串截取:right(str, length)  
  10. mysql> select right("sqlstudy.com", 3);  
  11. +--------------------------+  
  12. | right("sqlstudy.com", 3) |  
  13. +--------------------------+  
  14. | com                      |  
  15. +--------------------------+  
  16. 3. 字符串截取:substring(str, pos); substring(str, pos, len)  
  17. 3.1 从字符串的第 4 个字符位置开始取,直到结束。  
  18. mysql> select substring("sqlstudy.com", 4);  
  19. +------------------------------+  
  20. | substring("sqlstudy.com", 4) |  
  21. +------------------------------+  
  22. | study.com                    |  
  23. +------------------------------+  
  24. 3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。  
  25. mysql> select substring("sqlstudy.com", 4, 2);  
  26. +---------------------------------+  
  27. | substring("sqlstudy.com", 4, 2) |  
  28. +---------------------------------+  
  29. | st                              |  
  30. +---------------------------------+  
  31. 3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。  
  32. mysql> select substring("sqlstudy.com", -4);  
  33. +-------------------------------+  
  34. | substring("sqlstudy.com", -4) |  
  35. +-------------------------------+  
  36. | .com                          |  
  37. +-------------------------------+  
  38. 3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。  
  39. mysql> select substring("sqlstudy.com", -4, 2);  
  40. +----------------------------------+  
  41. | substring("sqlstudy.com", -4, 2) |  
  42. +----------------------------------+  
  43. | .c                               |  
  44. +----------------------------------+  
  45. 我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。  
  46. 4. 字符串截取:substring_index(str,delim,count)  
  47. 4.1 截取第二个 "." 之前的所有字符。  
  48. mysql> select substring_index("www.sqlstudy.com.cn", ".", 2);  
  49. +------------------------------------------------+  
  50. | substring_index("www.sqlstudy.com.cn", ".", 2) |  
  51. +------------------------------------------------+  
  52. | www.sqlstudy                                   |  
  53. +------------------------------------------------+  
  54. 4.2 截取第二个 "." (倒数)之后的所有字符。  
  55. mysql> select substring_index("www.sqlstudy.com.cn", ".", -2);  
  56. +-------------------------------------------------+  
  57. | substring_index("www.sqlstudy.com.cn", ".", -2) |  
  58. +-------------------------------------------------+  
  59. | com.cn                                          |  
  60. +-------------------------------------------------+  
  61. 4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串  
  62. mysql> select substring_index("www.sqlstudy.com.cn", ".coc", 1);  
  63. +---------------------------------------------------+  
  64. | substring_index("www.sqlstudy.com.cn", ".coc", 1) |  
  65. +---------------------------------------------------+  
  66. | www.sqlstudy.com.cn                               |  
  67. +---------------------------------------------------+  
  68.  4.4 截取一个表某个字段数据的中间值 如该字段数据为  1,2,3  
  69. mysql> select substring_index(substring_index(该字段, ",", 2) , ",", -1) from 表名;    
  70. +--------------------------------------------------------------+    
  71. | substring_index(substring_index(该字段, ",", 2);  , ",", -1)|    
  72. +--------------------------------------------------------------+    
  73. | 2                                        |    
  74. +--------------------------------------------------------------+   
  75.    
  76. ASCII(str)  
  77.      返回字符串str的最左面字符的ASCII代码值。如果str是空字符串,返回0。如果str是NULL,返回NULL。  
  78.    
  79. Java代码  
  80. 如:select ASCII("2");select ASCII(2); 结果都为2的Ascii码值 50     
  81. 如:select ASCII("2");select ASCII(2); 结果都为2的Ascii码值 50   
  82.    
  83. CHAR(N,...)  
  84. CHAR()将参数解释为整数并且返回由这些整数的ASCII代码字符组成的一个字符串。NULL值被跳过。  
  85.    
  86. Java代码  
  87. select CHAR(77,121,83,81,NULL);  结果为 MySQ ;   
  88. select CHAR(77,121,83,81,NULL);  结果为 MySQ ;  
  89.    
  90. [color=darkred]CONCAT(str1,str2,...)  
  91.     返回来自于参数连结的字符串。如果任何参数是NULL,返回NULL。可以有超过2个的参数。一个数字参数被变换为等价的字符串形式。  
  92.    
  93. Java代码  
  94. select CONCAT("My", "S", "QL");  //MySQL    
  95. select CONCAT(12.3); //"12.3"   
  96. select CONCAT("My", "S", "QL");  //MySQL  
  97. select CONCAT(12.3); //"12.3"  
  98.    
  99. LENGTH(str)  
  100.    计算字符串长度 :select length("text") ;  //4  
  101.    
  102. LOCATE(substr,str)    
  103. POSITION(substr IN str)  
  104. 返回子串substr在字符串str第一个出现的位置,如果substr不是在str里面,返回0.  
  105.    
  106. Java代码  
  107. 1.mysql> select LOCATE("bar", "foobarbar");    
  108. 2.        -> 4   
  109. 3.mysql> select LOCATE("xbar", "foobar");    
  110. 4.        -> 0   
  111. mysql> select LOCATE("bar", "foobarbar");  
  112.         -> 4  
  113. mysql> select LOCATE("xbar", "foobar");  
  114.         -> 0该函数是多字节可靠的  
  115.    
  116. LOCATE(substr,str,pos)  
  117. 返回子串substr在字符串str第一个出现的位置,从位置pos开始。如果substr不是在str里面,返回0。  
  118.    
  119. Java代码  
  120. 1.mysql> select LOCATE("bar", "foobarbar",5);    
  121. 2.        -> 7   
  122. mysql> select LOCATE("bar", "foobarbar",5);  
  123.         -> 7  
  124.    
  125. INSTR(str,substr)  
  126. 返回子串substr在字符串str中的第一个出现的位置。这与有2个参数形式的LOCATE()相同,除了参数被颠倒。  
  127.    
  128. Java代码  
  129. 1.mysql> select INSTR("foobarbar", "bar");    
  130. 2.        -> 4   
  131. 3.mysql> select INSTR("xbar", "foobar");    
  132. 4.        -> 0   
  133. mysql> select INSTR("foobarbar", "bar");  
  134.         -> 4  
  135. mysql> select INSTR("xbar", "foobar");  
  136.         -> 0这函数是多字节可靠的。  
  137.    
  138. LPAD(str,len,padstr)  
  139. 返回字符串str,左面用字符串padstr填补直到str是len个字符长。  
  140.    
  141. Java代码  
  142. 1.<PRE class=java name="code">mysql> select LPAD("hi",4,"??");    
  143. 2.        -> "??hi"   
  144. 3. [color=darkred]RPAD(str,len,padstr)[/color] </PRE>返回字符串str,右面用字符串padstr填补直到str是len个字符长。      
  145. Java代码 mysql> select LPAD("hi",4,"??");           -> "??hi"   
  146. mysql> select LPAD("hi",4,"??");  
  147.         -> "??hi"  
  148. [color=darkred]RPAD(str,len,padstr)[/color]  
  149. 返回字符串str,右面用字符串padstr填补直到str是len个字符长。    
  150. mysql> select RPAD("hi",5,"?");  
  151.         -> "hi???"  
  152. LEFT(str,len)  
  153. 返回字符串str的最左面len个字符。  
  154.    
  155. Java代码  
  156. 1.mysql> select LEFT("foobarbar", 5);    
  157. 2.        -> "fooba"该函数是多字节可靠的。   
  158. mysql> select LEFT("foobarbar", 5);  
  159.         -> "fooba"该函数是多字节可靠的。  
  160. RIGHT(str,len)  
  161. 返回字符串str的最右面len个字符。  
  162.    
  163. Java代码  
  164. 1.mysql> select RIGHT("foobarbar", 4);    
  165. 2.        -> "rbar"   
  166. mysql> select RIGHT("foobarbar", 4);  
  167.         -> "rbar"  
  168. 该函数是多字节可靠的。  
  169.    
  170. SUBSTRING(str,pos)  
  171.    
  172. SUBSTRING(str FROM pos)  
  173. 从字符串str的起始位置pos返回一个子串。  
  174. mysql> select SUBSTRING("Quadratically",5);  
  175.         -> "ratically"  
  176. mysql> select SUBSTRING("foobarbar" FROM 4);  
  177.         -> "barbar"  
  178. 该函数是多字节可靠的。  
  179. SUBSTRING_INDEX(str,delim,count)  
  180. 返回从字符串str的第count个出现的分隔符delim之后的子串。如果count是正数,返回最后的分隔符到左边(从左边数) 的所有字符。如果count是负数,返回最后的分隔符到右边的所有字符(从右边数)。  
  181. mysql> select SUBSTRING_INDEX("www.mysql.com", ".", 2);  
  182.         -> "www.mysql"  
  183. mysql> select SUBSTRING_INDEX("www.mysql.com", ".", -2);  
  184.         -> "mysql.com"  
  185. 该函数对多字节是可靠的。  
  186. LTRIM(str)  
  187. 返回删除了其前置空格字符的字符串str。  
  188. mysql> select LTRIM("  barbar");  
  189.         -> "barbar"  
  190. RTRIM(str)  
  191. 返回删除了其拖后空格字符的字符串str。  
  192. mysql> select RTRIM("barbar   ");  
  193.         -> "barbar"  
  194. 该函数对多字节是可靠的。   
  195. TRIM([[BOTH | LEADING | TRAILING] [remstr] FROM] str)  
  196. 返回字符串str,其所有remstr前缀或后缀被删除了。如果没有修饰符BOTH、LEADING或TRAILING给出,BOTH被假定。如果remstr没被指定,空格被删除。  
  197. mysql> select TRIM("  bar   ");  
  198.         -> "bar"  
  199. mysql> select TRIM(LEADING "x" FROM "xxxbarxxx");  
  200.         -> "barxxx"  
  201. mysql> select TRIM(BOTH "x" FROM "xxxbarxxx");  
  202.         -> "bar"  
  203. mysql> select TRIM(TRAILING "xyz" FROM "barxxyz");  
  204.         -> "barx"  
  205. 该函数对多字节是可靠的。  
  206. SOUNDEX(str)  
  207. 返回str的一个同音字符串。听起来“大致相同”的2个字符串应该有相同的同音字符串。一个“标准”的同音字符串长是4个字符,但是SOUNDEX()函数返回一个任意长的字符串。你可以在结果上使用SUBSTRING()得到一个“标准”的 同音串。所有非数字字母字符在给定的字符串中被忽略。所有在A-Z之外的字符国际字母被当作元音。  
  208. mysql> select SOUNDEX("Hello");  
  209.         -> "H400"  
  210. mysql> select SOUNDEX("Quadratically");  
  211.         -> "Q36324"  
  212. SPACE(N)  
  213. 返回由N个空格字符组成的一个字符串。  
  214. mysql> select SPACE(6);  
  215.         -> "      "  
  216. REPLACE(str,from_str,to_str)  
  217. 返回字符串str,其字符串from_str的所有出现由字符串to_str代替。  
  218. mysql> select REPLACE("www.mysql.com", "w", "Ww");  
  219.         -> "WwWwWw.mysql.com"  
  220. 该函数对多字节是可靠的。  
  221. REPEAT(str,count)  
  222. 返回由重复countTimes次的字符串str组成的一个字符串。如果count <= 0,返回一个空字符串。如果str或count是NULL,返回NULL。  
  223. mysql> select REPEAT("MySQL", 3);  
  224.         -> "MySQLMySQLMySQL"  
  225. REVERSE(str)  
  226. 返回颠倒字符顺序的字符串str。  
  227. mysql> select REVERSE("abc");  
  228.         -> "cba"  
    声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。