搜尋字元或者字串 CHARINDEX函式

2021-06-01 20:03:49 字數 1523 閱讀 8338

在sql中,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。

例一:customname包含客戶的first name和last name,它們之間被乙個空格隔開。我們用charindx函式確定兩個名字中間空格的位置。通過這個方法,我們可以分析contactname列的空格位置,這樣可以只顯示這個列的last name部分。  

select top 5 substring(contactname,charindex(' ',contactname)+1,len(contactname)) as [last name] from customers  charindex函式找到first name和last name之間的空格,所以substring函式可以分開contactname列,這樣就只有last name被選出。在charindex函式返回的整數上加1,這樣last name不是從空格開始。例二:計算northwind.dbo.customer表中addresses欄位中包含單詞road或者它的縮寫rd的記錄數,選擇語句類似這樣:  

select count(*) from northwind.dbo.customers where charindex('rd',address) > 0 or charindex('road',address)> 0在oracle 10g中無法使用。提示charindex

識別符號無效

字串搜尋

include include include includeusing namespace std char grid 100 100 word 100 int n m int x int y int xx yy void search if k len int main int t cin t ...

字串搜尋

include include include includeusing namespace std char grid 100 100 word 100 int n m int x int y int xx yy void search if k len int main int t cin t ...

字串搜尋樹

字串搜尋樹支援使用字串對物件建立索引,以便於查詢。這些字串應該都是從物件的屬性上獲取的。而且可以按照指定的規則將字串分解為單詞。insert方法用來在乙個單詞和指定物件之間建立關聯。remove方法用來在索引樹中刪除乙個指定物件。prune方法用於在刪除了大量物件後,優化索引樹,刪除那些沒有作用的節...