首页 > 数据库技术 > 详细

常用数据库字符串处理函数举例

时间:2015-11-03 22:57:59      阅读:565      评论:0      收藏:0      [点我收藏+]

1、字符串拼接函数

  MySQL:select concat(‘123‘,‘456‘);    可以链接多个字符串。其中只要一个为null,则结果为null。

  SQLServer:select ‘123‘+‘456‘;  

  Oracle:select ‘123‘||‘456‘ from dual;  可链接多个。  select concat(‘123‘,‘456‘) from dual;只能链接两个,只要有一个不为null,则结果就不为null。

  MySQL:concat_ws(分隔符,字符串1,字符串2,...)函数, 表示concat with separator,即有分隔符的字符串连接。

 

2、字符串截取函数

  SQLServer:SUBSTRING(string,start_position,length)   需要注意的是这里的start_position是从1开始,不同于数组是从0开始。

  Oracle:substr( string, start_position, [ length ] )   substr(‘目标字符串‘,开始位置,长度),从1开始。写成0的话效果同1。

  MySQL:left(str, length); right(str, length); substring(str, pos); substring(str, pos, len);  select substring_index(‘www.example.com‘, ‘.‘, 2); 截取第二个 ‘.‘ 之前的所有字符。得到 ‘www.example‘。subString起始位为1,如果写为0,返回空白字符串。

 

3、字符串查找函数

  MySQL:INSTR(str,substr); 返回子串substr在字符串str中的第一个出现的位置。

      select INSTR(‘foobarbar‘, ‘bar‘);

      -> 4
      select INSTR(‘xbar‘, ‘foobar‘);
      -> 0

  SQLServer:CHARINDEX ( expression1 , expression2 [ , start_location ] ); Expression1是要到expression2中寻找的字符中,start_location是CHARINDEX函数开始在expression2中找expression1的位置。

  Oracle:INSTR(源字符串, 目标字符串, [起始位置], [匹配序号]); 例如:INSTR(‘CORPORATE FLOOR‘,‘OR‘, 3, 2)中,源字符串为‘CORPORATE FLOOR‘, 目标字符串为‘OR‘,起始位置为3,取第2个匹配项的位置。

常用数据库字符串处理函数举例

原文:http://www.cnblogs.com/moleme/p/4934542.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!