SQL截取字符串
GPS平台、网站建设、软件开发、系统运维,找森大网络科技!
https://cnsendnet.taobao.com
来自森大科技官方博客
http://www.cnsendblog.com/index.php/?p=2022
SQL使用charindex和substring截取字符串
SUBSTRING 
返回字符、binary、text 或 image表达式的一部分。有关可与该函数一起使用的有效Microsoft SQL Server 数据类型的更多信息,请参见数据类型。   
语法 
SUBSTRING(expression,starting_position int,length int)   
参数 
expression 
是要从哪个字符串中截取就写那个字符串、二进制字符串、text、image、列或包含列的表达式。不要使用包含聚合函数的表达式。 
starting_position
是一个整数,指定子串的开始位置。 
注意:开始位置是从1开始,如abcdefg中c是第3个。
length 
是一个整数,指定子串的长度(要返回的字符数或字节数)。
substring() ——任意位置取子串 
left() 
right() 
——左右两端取子串 
ltrim() 
rtrim() 
——截断空格,没有trim()。 
charindex() 
patindex() 
——查子串在母串中的位置,没有返回0。区别:patindex支持通配符,charindex不支持。
函数功效:
字符串截取函数,只限单字节字符使用(对于中文的截取时遇上奇数长度是会出现乱码,需另行处理),本函数可截取字符串指定范围内的字符。
应用范围:
标题、内容截取
函数格式:
string substr(string string, int start [, int length])
参数1:处理字符串
参数2:截取的起始位置(第一个字符是从0开始)
参数3:截取的字符数量
substr()更多介绍可在PHP官方手册中查询(字符串处理函数库)
1.截取已知长度的函数
  A.截取从字符串左边开始N个字符
| 
      Declare @S1 varchar(100)  | 
 
B.截取从字符串右边开始N个字符(例如取字符www.163.com)
| 
      Declare @S1 varchar(100)  | 
 
C.截取字符串中任意位置及长度(例如取字符www)
| 
      Declare @S1 varchar(100)  | 
 
     以上例子皆是已知截取位置及长度,下面介绍未知位置的例子
2.截取未知位置的函数
  A.截取指定字符串后的字符串(例如截取http://后面的字符串)
方法一:
| 
      Declare @S1 varchar(100)     
  ------------------------------------  | 
 
需要注意:CHARINDEX函数搜索字符串时,不区分大小写,因此CHARINDEX('www',@S1)也可以写成CHARINDEX('WWW',@S1)
方法二:(与方法一类似)
| 
      Declare @S1 varchar(100)  | 
 
函数PATINDEX与CHARINDEX区别在于:前者可以参数一些参数,增加查询的功能
方法三:
| 
      Declare @S1 varchar(100)  | 
 
利用字符替换函数REPLACE,将除需要显示字符串外的字符替换为空
方法四:
| 
      Declare @S1 varchar(100)  | 
 
   函数STUFF与REPLACE区别在于:前者可以指定替换范围,而后者则是全部范围内替换
  B.截取指定字符后的字符串(例如截取C:\Windows\test.txt中文件名)
       与A不同的是,当搜索对象不是一个时,利用上面的方法只能搜索到第一个位置
方法一:
| 
      Declare @S1 varchar(100)  | 
 
利用函数REVERSE获取需要截取的字符串长度
substr()
例子:
private void DDL_AreaBind()
          {
             
conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["strcon"].ConnectionString);
             
string str = "0000";
             
cmd = new SqlCommand("select AreaID,Name=ltrim(Name) from Area
where    right(AreaID,4) ='" + str + "'", conn);
             
SqlDataAdapter sda = new SqlDataAdapter(cmd);
             
sda.Fill(ds, "area");
             
this.ddl_area.DataSource = ds.Tables["area"].DefaultView;
             
this.ddl_area.DataTextField = "Name";
             
this.ddl_area.DataValueField = "AreaID";
             
this.ddl_area.DataBind();
            
             
cmd = new SqlCommand("select * from Area    ", conn);
             
cmd.CommandType = CommandType.Text;
             
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
             
adapter.Fill(ds, "city");
             
this.ddl_city.DataSource = ds.Tables["city"].DefaultView;
             
this.ddl_city.DataTextField = "Name";
             
this.ddl_city.DataValueField = "AreaID";
             
this.ddl_city.DataBind();
          }
protected void ddl_area_SelectedIndexChanged(object
sender, EventArgs e)
          {
             
conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["strcon"].ConnectionString);
             
this.ddl_city.Enabled = true;
             
string str1="0000";
             
cmd = new SqlCommand("select AreaID,Name from Area where
substring(AreaID,1,2)='" +
this.ddl_area.SelectedValue.Substring(0,2)    + "' AND
substring(AreaID,3,4) <> '0000' AND
substring(AreaID,5,2)='00'    ", conn);
             
cmd.CommandType = CommandType.Text;
             
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
             
DataSet ds = new DataSet();
             
adapter.Fill(ds, "city");
             
this.ddl_city.DataSource = ds.Tables["city"].DefaultView;
             
this.ddl_city.DataTextField = "Name";
             
this.ddl_city.DataValueField = "AreaID";
             
this.ddl_city.DataBind();
          }
PS:
最近项目中用到比较少见的SQL语句,分享一下:
查询祖先节点
select * from 目录表_数据库 where
ID<>-1 and datatype<>1 and datatype<>2 connect by prior
FATHERID=ID start with ID=28 order by 目录级别,ID
查询子孙节点:
select * from 目录表_数据库 where
ID<>-1 and datatype<>1 and datatype<>2 connect by prior
ID=FATHERID start with ID=28 order by 目录级别,ID
select * from table_a where charindex('a',id)>0
or charindex('b',id)>0 
table_a 表中 id字段中含有"a"或者"b"的记录. 
--------------------------------------------------- 
CHARINDEX 
返回字符串中指定表达式的起始位置。 
语法 
CHARINDEX ( expression1 , expression2 [ , start_location ] ) 
参数 
expression1 
一个表达式,其中包含要寻找的字符的次序。expression1 是一个短字符数据类型分类的表达式。 
expression2 
一个表达式,通常是一个用于搜索指定序列的列。expression2 属于字符串数据类型分类。 
start_location 
在 expression2 中搜索 expression1 时的起始字符位置。如果没有给定 start_location,而是一个负数或零,则将从 expression2 的起始位置开始搜索。 
返回类型 
int 
注释 
如果 expression1 或 expression2 之一属于 Unicode 数据类型(nvarchar 或 nchar)而另一个不属于,则将另一个转换为 Unicode 数据类型。 
如果 expression1 或 expression2 之一为 NULL 值,则当数据库兼容级别为 70 或更大时,CHARINDEX 返回 NULL 值。当数据库兼容级别为 65 或更小时,CHARINDEX 仅在 expression1 和 expression2 都为 NULL 时返回 NULL 值。 
如果在 expression2 内没有找到
expression1,则 CHARINDEX 返回 0。 
示例 
第一个代码示例返回序列"wonderful"在 titles 表的 notes 列中开始的位置。第二个示例使用可选的 start_location 参数从 notes 列的第五个字符开始寻找"wonderful"。第三个示例显示了当 expression2 内找不到 expression1 时的结果集。 
USE pubs 
GO 
SELECT CHARINDEX(’wonderful’, notes) 
FROM titles 
WHERE title_id = ’TC3218’
GO 
-- Use the optional start_location parameter to start searching 
-- for wonderful starting with the fifth character in the notes 
-- column. 
USE pubs 
GO 
SELECT CHARINDEX(’wonderful’, notes, 5) 
FROM titles 
WHERE title_id = ’TC3218’
GO 
下面是第一个查询和第二个查询的结果集: 
----------- 
46           
(1 row(s) affected) 
USE pubs 
GO 
SELECT CHARINDEX(’wondrous’, notes) 
FROM titles 
WHERE title_id=’TC3218’
GO 
下面是结果集。 
----------- 
0         
GPS平台、网站建设、软件开发、系统运维,找森大网络科技!
https://cnsendnet.taobao.com
来自森大科技官方博客
http://www.cnsendblog.com/index.php/?p=2022
                    
                
                
            
        
浙公网安备 33010602011771号