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