SQL 字串處理函式

2021-06-08 14:51:41 字數 2914 閱讀 9936

--將字串中從某個字元開始擷取一段字元,然後將另外乙個字串插入此處

select stuff('hello,world!',4,4,'****')   --返回值hel****orld!

--返回從指定位置開始指定長度的字串

select substring('hello,world!',2,10)   --返回值ello,world

--將字串中某段字元替換為指定的字串

select replace('hello,world!','ll','aa') --返回值heaao,world!

--去除字串中左邊的空格

select ltrim('   hello,world!')    --返回值hello,world!

--去除字串中左邊的空格

select ltrim('hello,world!   ')    --返回值hello,world!

--去除字串中左邊和右邊的空格

select ltrim('    hello,world!   ')   --返回值hello,world!

--將null值替換為指定字元

select isnull('a',null)     --返回值a

--轉換資料型別

select cast('2007-10-11' as datetime)   --返回值2007-10-11 00:00:00.000

select convert(datetime,'2007-10-11')   --返回值2007-10-11 00:00:00.000

--獲取字串長度

select len('hello,world!')    --返回值12

--獲取字串的前3個字元

select left('hello,world!',3)    --返回值hel

--獲取字串的後3個字元

select right('hello,world!',3)    --返回值ld!

--去除字串的前3個字元

select right('hello,world!',(len('hello,world!')-3)) --返回值lo,world!

--去除字串的後3個字元

select left('hello,world!',(len('hello,world!')-3)) --返回值hello,wor

--獲取在該字串中某字串的位置(返回數字)

select charindex('e','hello,world!')   --返回值2

--返回從第2個字元開始前4個字元

select left(right('[哈哈哈哈]aaa',len('[哈哈哈哈]aaa')-1),4) --返回值哈哈哈哈

--返回字元的小寫形式

select lower('hello,world!')    --返回值hello,world!

--返回字元的大寫形式

select upper('hello,world!')    --返回值hello,world!

--用第三個表示式替換第乙個字串表示式中出現的所有第二個指定字串表示式的匹配項

(如果其中有乙個輸入引數屬於 nvarchar 資料型別,則返回 nvarchar;否則返回 varchar。如果任何乙個引數為 null,則返回 null。)

select replace('hello,world!','l','a')   --返回值heaao,worad!

select replace('hello,world!','l','')   --返回值heo,word!

select replace('hello,world!','l',null)   --返回值null

--以右邊引數數值次數複製字元表示式

select replicate('hello,world!',4)   --返回值hello,world!hello,world!hello,world!hello,world!

--返回反轉後的字串

select reverse('hello,world!')    --返回值!dlrow,olleh

--使用difference時,兩個字串發音越相似(僅限於英文本元),返回值越大(返回值在0-4之間)

difference('sun','san')    --返回值4

difference('sun','safdsdf')   --返回值3

difference('sun','dgffgfdg')   --返回值0

--將帶小數點的數字型別轉換為可設定長度可設定小數字的四捨五入後的字串

select str(123.34584, 7, 3)   --返回值123.346

--當設定長度值小於整數部位長度時,字串將返回設定長度個*

select str(123333.34584, 5, 4)   --返回值*****

--***********************************==數字操作彙總******************************====

--返回指定數字的最大整數

select floor(123456.1234)   --返回值123456

--返回不帶小數部分並且不小於其引數的值的最小數字。如果引數是乙個空序列,則返回空序列

select ceiling(123.010)    --返回124

select ceiling(null)    --返回null

--返回四捨五入後的最接近該數值的數值

select round(126.018,2)    --返回126.12

--返回乙個0-1之間的float型別的隨機數

select rand()     --返回0.94170703697981

--返回圓周率pi的值

select pi()     --返回3.14159265358979

原文出處:

sql字串處理函式

sql字串函式 2007年05月15日 星期二 09 05 sql字串函式 字串函式對二進位制資料 字串和表示式執行不同的運算。此類函式作用於char varchar binary 和varbinary 資料型別以及可以隱式轉換為char 或varchar的資料型別。可以在select 語句的sel...

sql字串處理函式

1 upper 函式 upper characer expression characer expression 是由字元資料組成的表示式,可將表示式中部分全部轉成大寫字母 如 select upper hello output hello 2 initcap 函式 initcap expressi...

SQL字串處理函式

字串函式對二進位制資料 字串和表示式執行不同的運算。此類函式作用於char varchar binary 和varbinary 資料型別以及能夠隱式轉換為char 或varchar的資料型別。一 字元轉換函式 1 ascii 與char ascii 返回字元表示式最左端字元的ascii 碼值。在as...