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