close
SQL 常用函數

字串函數

len(字串運算式):傳回字串運算式的字元數目(非位元數),並去除字串最後的連續空白。

select len(nm) from student

lower(字串運算式)將字串運算式中的大寫字母轉換為小寫字母後回傳。

select lower(nm) from student

upper(字串運算式)將字串運算式中的小寫字母轉換為大寫字母後回傳。

select upper(nm) from student

ltrim(字串運算式)去除字串運算式前面的連續空白。

select ltrim('      ABCDEFG')

rtrim(字串運算式)去除字串運算式後面的連續空白。

select rtrim('      ABCDEFG      ')+'HIJKLMN'

reverse(字串運算式)反轉排列字串運算式後回傳。

select reverse('台中勤益科技大學')

數學函數

1.           abs(運算式)傳回運算式的絕對值。

2.          ceiling(運算式)傳回運算式中大於或等於的最小整數。

3.          floor(運算式)傳回運算式中小於或等於的最大整數。

4.          found(運算式 , 四捨五入的長度)傳回運算式四捨五入後的值。

日期時間函數

1.           day(日期)傳回指定日期的日數為何。

2.          month(日期)傳回指定日期的月份為何。

3.          year(日期)傳回指定日期的年份為何。

4.          getdate()傳回目前的系統日期與時間。

聚合函數 (Aggregate Function)

簡單的說聚合函數就是針對不同分類的統計函數,大多搭配 group by 一起使用。

count(expr)計算分類中的紀錄筆數但不包含 Null 的欄位,除非 expr 是星號 (*) 萬用字元。

select count(*) from student

select dptcd, count(*) from student group by dptcd --依照group分組後的分類集合做統計

select s.dptcd , d.dptnm , count(s.*) from student s, department d where s.dptcd=d.dptcd group by s.dptcd --依照group分組後的分類集合做統計,並join科系名稱

sum():傳回分類中的數值總和。

select sum(tall) from student

select sum(tall) from student group by dptcd

select s.dptcd , d.dptnm , sum(s.tall) from student s, department d where s.dptcd=d.dptcd group by s.dptcd

avg():傳回分類中的數值平均。

select avg(tall) from student

select avg(tall) from student group by dptcd

select s.dptcd , d.dptnm , avg(s.tall) from student s, department d where s.dptcd=d.dptcd group by s.dptcd

min():傳回分類中的數值最小值。

select min(tall) from student

select min(tall) from student group by dptcd

select s.dptcd , d.dptnm , min(s.tall) from student s, department d where s.dptcd=d.dptcd group by s.dptcd

max():傳回分類中的數值最大值。

select max(tall) from student

select max(tall) from student group by dptcd

select s.dptcd , d.dptnm , max(s.tall) from student s, department d where s.dptcd=d.dptcd group by s.dptcd

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 snowman 的頭像
    snowman

    snowman的部落格

    snowman 發表在 痞客邦 留言(0) 人氣()