sql擷取字串和相關函式使用

2021-09-02 04:09:10 字數 930 閱讀 9005

1、

sql中的函式charindex():

charindex 函式

返回字元或者字串在另乙個字串中的起始位置。

charindex 函式呼叫方法如下:

charindex ( expression1 , expression2 [ , start_location ] )

expression1 是要到 expression2 中尋找的字元中,start_location 是 charindex 函式開始在 expression2 中找expression1 的位置。

charindex 函式返回乙個整數,返回的整數是要找的字串在被找的字串中的位置。假如 charindex 沒有找到要找的字串,那麼函式整數「0」。

使用方法:

select charindex('-',condate) from contribution

返回"-"在condate中第一次出現的位置。

參考文章:

2、

sql中的函式substring():

select substring('abcdefg',1,4)

得到的結果是abcd,即從第一位開始,擷取4位

使用方法:

select substring(condate,1,4) from contribution

擷取condate欄位,從第乙個字元開始擷取,擷取四個字元。

參考文件:

3、

sql函式substring(),charindex()聯合使用:

select * from contribution where convert(int,substring(condate,charindex('-',condate)+1,4))>2012

找到condate段中"-"的位置,然後從該位置開始,擷取4個字串,然後轉換為int,和2012比較大小。

SQL擷取字串函式

a.擷取從字串左邊開始n個字元 以下是 片段 declare s1 varchar 100 select s1 select left s1,4 顯示結果 http b.擷取從字串右邊開始n個字元 例如取字元www.163.com 以下是 片段 declare s1 varchar 100 sele...

SQL擷取字串函式

這裡介紹三種擷取函式。left string,length 函式 從字串string中,從第乙個字元開始,自左向右,擷取長度為length的子串。舉個例子,string為 abcdefg length為3,那麼擷取的子串就是 abc 了。right string,length 函式 與上面函式類似,...

SQL擷取字串函式

a.擷取從字串左邊開始n個字元 declare s1 varchar 100 select s1 select left s1,4 顯示結果 http b.擷取從字串右邊開始n個字元 例如取字元www.163.com declare s1 varchar 100 select s1 select r...