佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > SQLServer之常用函數(shù)總結(jié)詳解

SQLServer之常用函數(shù)總結(jié)詳解

熱門標(biāo)簽:呼叫中心市場(chǎng)需求 Linux服務(wù)器 AI電銷 網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng) 服務(wù)外包 百度競(jìng)價(jià)排名 地方門戶網(wǎng)站

SQLServer中的常用函數(shù)

字符串函數(shù)

len() 計(jì)算字符串的長度

select LEN(name) from test1  --計(jì)算name的長度

大小寫轉(zhuǎn)換 lower() upper()

select lower('STUDENT !')
select upper('student !')

去空 ltrim() 字符串左側(cè)的空格去掉 ,rtrim()字符串右側(cè)的空格去掉

declare @str varchar(100) = ' a a a '
select ltrim(@str)
select rtrim(@str)

字符串截取 substring() left() right()

select substring('HelloWorld!',6,6)  --可截取任意長度
select left('HelloWorld!' ,5)  --從左開始截取
select right('HelloWorld!' ,6) --從右開始截取

字符串替換 replace()

select replace('HelloWorld!','o','e')    --string,要被替換的字符串,替換的字符串

字符串 掉個(gè)順序 reverse()

select reverse('abc')    --cba

返回 字符串1在字符串2中出現(xiàn)的未位置 charindex()

charindex(srt1 ,srt2)--srt1 在srt2中的開始位置

select charindex('H','elloHWorld') 結(jié)果為:5 --只能查第一次出現(xiàn)的位置,匹配不到返回0

指定的次數(shù)重復(fù)字符串值 replicate()

select replicate('abc',4) 結(jié)果為:abcabcabcabc

聚合函數(shù)

聚合函數(shù)對(duì)一組值計(jì)算后返回單個(gè)值。除了count(統(tǒng)計(jì)項(xiàng)數(shù))函數(shù)以外,其他的聚合函數(shù)在計(jì)算式都會(huì)忽略空值(null)。所有的聚合函數(shù)均為確定性函數(shù)。

平均值 avg() 算一組數(shù)的總和,然后除以為null的個(gè)數(shù),得到平均值。

select avg(id) from test1     avg(列名)

最小值min() 最大值max()

select min(id) from test1
select max(id) from test1

求和 sum()

select sum(id) from test1

計(jì)算總數(shù) count()

select count(id) from test1

分組

統(tǒng)計(jì)學(xué)生的總成績并排序
select stu_id as 學(xué)生編號(hào) ,name as 學(xué)生姓名 , SUM(語文+英語+數(shù)學(xué)+代數(shù)) as 總分from tb_stuAchievement 
ORDER BY 總分 DESC
GROUP BY stu_id ,name 

(函數(shù)可能不全,我只記錄了我用到的,完整的函數(shù)可以查查手冊(cè))

日期和時(shí)間函數(shù)

獲取當(dāng)前日期GetDate

select getdate() 

GetUTCDate 獲取UTC時(shí)間值

select GETUTCDATE()

單獨(dú)獲取年月日

select year(getdate())
select month(getdate())
select day(getdate())

日期減法 DATEDIFF

select datediff(YYYY,'2011-11-11','2012-12-12')    --輸出1 年份相減之后的確是1
select datediff(day,'2011-11-11','2012-12-12')     --輸出 397 兩個(gè)日期相差的天數(shù)

SQLServer 2008中新增的日期時(shí)間型函數(shù)

1、獲取系統(tǒng)時(shí)間 SysDateTime()
2、獲取當(dāng)前日期和時(shí)間 SysDateTimeOffset
3、獲取系統(tǒng)UTC時(shí)間 SysUTCDateTime
4、Current_TimeStamp當(dāng)前數(shù)據(jù)庫系統(tǒng)時(shí)間戳
5、判斷是否為日期數(shù)據(jù)isDate
		select isdate('2012-12-12')    -- 輸出1
     select isdate('xxxx-12-12')  -- 輸出0

(函數(shù)可能不全,我只記錄了部分,完整的函數(shù)可以查查手冊(cè))

MID() 從文本字段中提取字符。

SELECT MID(City,1,3) as SmallCity FROM Persons

ROUND() 函數(shù) 數(shù)值字段舍入為指定的小數(shù)位數(shù)。

SELECT ROUND(column_name,decimals) FROM table_name

NOW() 函數(shù) 返回當(dāng)前的日期和時(shí)間。

SELECT NOW() FROM table_name

FORMAT () 用于對(duì)字段的顯示進(jìn)行格式化。

select FORMAT(stu_intime,'yyyy-MM-dd hh:mm:ss') as intime from stu

SELECT INTO 從一個(gè)表中選取數(shù)據(jù),然后把數(shù)據(jù)插入另一個(gè)表中。

select stu_name,stu_id into stu1 from stu   --將stu表中的stu_id和stu_name插入新表stu1(會(huì)創(chuàng)建一個(gè)新表)

sql server 中沒有l(wèi)imit 但是有top

/*查詢 從n開始的m條數(shù)據(jù) */
  declare @n int=2;
  declare @m int = 5;
  select top (@m) * from stu
  where id not in (select top (@n) id from stu)

  /*查詢n到m之間的數(shù)據(jù)*/
  declare @n int=2;
  declare @m int = 5;
  select top (@m-@n+1) * from stu
  where id not in (select top (@n-1) id from stu)

BETWEEN … AND 會(huì)選取介于兩個(gè)值之間的數(shù)據(jù)范圍。這些值可以是數(shù)值、文本或者日期。

/* 查詢id 1 到3 的數(shù)據(jù) */
  select * from stu where id between '1' and '3' 

ALTER TABLE 語句用于在已有的表中添加、修改或刪除列。

 alter table stu add stu_sj varchar(200)   --添加一列名為stu_sj

 alter table stu drop column stu_sj  --刪除列

DISTINCT 用于返回唯一不同的值。

/* 返回名字,重復(fù)的不返回 */
  select distinct stu_name from stu

到此這篇關(guān)于SQLServer之常用函數(shù)總結(jié)詳解的文章就介紹到這了,更多相關(guān)SQLServer之常用函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • sqlserver存儲(chǔ)過程中SELECT 與 SET 對(duì)變量賦值的區(qū)別
  • SQLServer中SELECT語句的執(zhí)行順序
  • SqlServer數(shù)據(jù)庫遠(yuǎn)程連接案例教程
  • SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解

標(biāo)簽:蘭州 湘潭 仙桃 衡水 湖南 崇左 銅川 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQLServer之常用函數(shù)總結(jié)詳解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    荥经县| 平定县| 通江县| 巴南区| 西平县| 陆丰市| 溧水县| 资阳市| 林州市| 阿克苏市| 浠水县| 拜城县| 峡江县| 丰顺县| 云林县| 南汇区| 博罗县| 太白县| 陈巴尔虎旗| 辽宁省| 福建省| 昆山市| 临海市| 沙洋县| 湖州市| 青冈县| 龙胜| 兰州市| 云龙县| 塔河县| 二手房| 济源市| 河津市| 贵德县| 深州市| 宝兴县| 维西| 晋城| 庐江县| 鹿邑县| 华阴市|