--SQL SERVER 2008 函數大全
/*
author:TracyLee
csdncount:Travylee
*/函數
/*
1、字符串函數:
一、ascii(字符串表達式)
返回字符串中最右側字符的ASCII碼
例:select ascii('abc')
return:97
二、char(字符串表達式)
把ASCII碼轉換成對應的字符
例:select char(97)
return:a
三、charindex(字符串表達式1,字符串表達式,2[,整數表達式])
字符串2中查找字符串1,若是存在返回第一個存在的位置,若是不存在,返回0
若是字符串1和字符串2中有一個爲null,返回null。整數表達式能夠指明在字
符串2中查找的起始位置
例:select charindex('a','basketball') --return:2
select charindex('a','basketball',3)--return:8
四、difference(字符串表達式1,字符串表達式2)
判斷兩個字符串的類似程度,返回0-4中的一個整數,0表示幾乎不類似或者徹底不
類似,4表示幾乎類似或者徹底類似
例:select difference('tracy','tracy')--return:4
select difference('kobe','tracy') --return:0
五、left(字符串表達式,整數表達式)
返回字符串表達式1中,從左邊開始,指定整數個數的字符
例:select left('tracy',2)--return:tr
六、right(字符串表達式,整數表達式)
返回字符串表達式1中,從右邊開始,指定整數個數的字符
例:select right('tracy',2)--return:cy
七、datalength(字符串表達式)
返回字符串表達式的字節數
例:select datalength('tracy')--return:5
select datalength('中國') --return:4
八、len(字符串表達式)
返回字符串表達式的字符數
例:select len('tracy')--返回值爲:5
select len('中國') --返回值爲:2
九、ltrim(字符串表達式)
去掉字符串表達式左邊的空格
例:select ltrim(' tracy')--return:tracy
十、rtrim(字符串表達式)
去掉字符串表達式右邊的空格
例:select rtrim('tracy ')--return:tracy
十一、substring(字符串,整數表達式1,整數表達式2)
從字符串中的整數表達式2位起截取整數表達式2長度的字符串
例:select substring('tracyleebaihe',3,2)--return:ac
十二、lower(字符串表達式)和upper(字符串表達式)
前者把字符串中的大寫字母轉換成小寫,後者反之.如字符串中不含有
字母,責返回原串
例:select lower('彈TTtt彈堂')--return:彈tttt彈堂
例:select upper('彈TTtt彈堂')--return:彈TTTT彈堂
例:select lower('字符串')--return:字符串
例:select upper('字符串')--return:字符串
1三、reverse('字符串表達式')
把字符串表達式倒置,返回倒置後的新串
例:select reverse('讀死書')--return:書死讀
1四、stuff(字符串表達式1,開始位置,長度,字符串表達式2)
在字符串表達式1中,從指定的開始位置刪除指定的長度的字符,並插
入指定的字符串表達式2
例:select stuff('tracyxxxbaihe',6,3,'lee')--return:tracyleebaihe
1五、replace(字符串表達式1,字符串表達式2,字符串表達式3)
用字符串表達式3替換字符串表達式1中出現的字符串表達式2,返回替
換後的新串
例:select replace('tracyxxxbaihe','xxx','lee')--return:tracyleebaihe
*/ci