SQL字串操作匯總

來源:互聯網
上載者:User
-===========字串使用匯總================

--將字串中從某個字元開始截取一段字元,然後將另外一個字串插入此處
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.