SQL字符串操作汇总
--將字符串中從某個字符開始截取一段字符,然后將另外一個字符串插入此處
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
--轉(zhuǎn)換數(shù)據(jù)類型
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
--獲取在該字符串中某字符串的位置(返回數(shù)字)
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!
--用第三個表達式替換第一個字符串表達式中出現(xiàn)的所有第二個指定字符串表達式的匹配項
(如果其中有一個輸入?yún)?shù)屬于 nvarchar 數(shù)據(jù)類型,則返回 nvarchar;否則返回 varchar。如果任何一個參數(shù)為 NULL,則返回 NULL。)
SELECT REPLACE('Hello,World!','l','a')?? --返回值Heaao,Worad!
SELECT REPLACE('Hello,World!','l','')?? --返回值Heo,Word!
SELECT REPLACE('Hello,World!','l',null)?? --返回值NULL
--以右邊參數(shù)數(shù)值次數(shù)復制字符表達式
select REPLICATE('Hello,World!',4)?? --返回值Hello,World!Hello,World!Hello,World!Hello,World!
--返回反轉(zhuǎn)后的字符串
select REVERSE('Hello,World!')??? --返回值!dlroW,olleH
--使用DIFFERENCE時,兩個字符串發(fā)音越相似(僅限于英文字符),返回值越大(返回值在0-4之間)
DIFFERENCE('sun','san')??? --返回值4
DIFFERENCE('sun','safdsdf')?? --返回值3
DIFFERENCE('sun','dgffgfdg')?? --返回值0
--將帶小數(shù)點的數(shù)字類型轉(zhuǎn)換為可設定長度可設定小數(shù)位的四舍五入后的字符串
SELECT STR(123.34584, 7, 3)?? --返回值123.346
--當設定長度值小于整數(shù)部位長度時,字符串將返回設定長度個*
SELECT STR(123333.34584, 5, 4)?? --返回值*****
--===================================================================================
--=====================================數(shù)字操作匯總==================================
--返回指定數(shù)字的最大整數(shù)
select floor(123456.1234)?? --返回值123456
--返回不帶小數(shù)部分并且不小于其參數(shù)的值的最小數(shù)字。如果參數(shù)是一個空序列,則返回空序列
select ceiling(123.010)??? --返回124
select ceiling(null)??? --返回NULL
--返回四舍五入后的最接近該數(shù)值的數(shù)值
select round(126.018,2)??? --返回126.12
--返回一個0-1之間的FLoat類型的隨機數(shù)
select rand()???? --返回0.94170703697981
--返回圓周率PI的值
SELECT PI()???? --返回3.14159265358979
總結(jié)
以上是生活随笔為你收集整理的SQL字符串操作汇总的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 堡垒之夜瓦尔基里之翼是什么
- 下一篇: 美的美居APP怎么查看房间湿度