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

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

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

SQLServer中的常用函數(shù)

字符串函數(shù)

len() 計算字符串的長度

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

大小寫轉換 lower() upper()

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

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

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,要被替換的字符串,替換的字符串

字符串 掉個順序 reverse()

select reverse('abc')    --cba

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

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

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

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

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

聚合函數(shù)

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

平均值 avg() 算一組數(shù)的總和,然后除以為null的個數(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

計算總數(shù) count()

select count(id) from test1

分組

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

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

日期和時間函數(shù)

獲取當前日期GetDate

select getdate() 

GetUTCDate 獲取UTC時間值

select GETUTCDATE()

單獨獲取年月日

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 兩個日期相差的天數(shù)

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

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

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

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ù) 返回當前的日期和時間。

SELECT NOW() FROM table_name

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

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

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

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

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 會選取介于兩個值之間的數(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 用于返回唯一不同的值。

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

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

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

標簽:蘭州 湘潭 仙桃 衡水 湖南 崇左 銅川 黃山

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

    • 400-1100-266