sql笔记四:函数

 

sql笔记四:函数
聚合函数:sum、avg、max、min、count
              Sum:求和:
                     格式:select sum(列名) from 表名
                     实例:select sum(age) from score
                     说明:求score表中age的总和。
              Avg:平均数:
                     格式:select avg(列名) from 表名
                     实例:select avg(age) from score
                     说明:求score表中age的平均值
              Max:最大值:
                     格式:select max(列名) from 表名
                     实例:select max(age) from score
                     说明:求score表中age的最大值
              Min:最小值:
                     格式:select min(列名) from 表名
                     实例:select min(age) from score
                     说明:求score表中age的最小值
              Count:统计数量:
                     格式:select count(列名) from 表名
                     实例:select count(age) from score
                     说明:求score表中age的数量
日期函数:getdate、dateadd、datediff、datepart
              Getdate:当前日期:
                     格式:getdate()
                     实例:select getdate()
                             Insert into score values(getdate())
                     说明:①得到当前时间,②向表格中插入当前时间。
              Dateadd:日期相加:
                     格式:dateadd(datepart,number,date)
                     实例:select dateadd(day,2,getdate)
                     说明:以当前时间为基础在天数上增加两天,datepart的参数有:year month day hour minute second
              Datediff:时间差:
                     格式:datediff(datepart,startdate,enddate)
                     实例:select datediff(day,’2008-08-08’,getdate)
                     说明:奥运会开幕距当前多少天了
              Datepart:返回时间一部分:
                     格式:datepart(datepart,date)
                     实例:select datepart(month,getdate)
                     说明:返回当前月份
数学函数:abs、rand、floor、ceiling、round、sqrt
              Abs:绝对值:
                     格式:abs()
                     实例:select abs(stock) from shop
                     说明:从shop表中选择stock库存量的绝对值
              Rand:随机函数:
                     格式:rand()
                     实例:select rand()
                     说明:返回0到1之间的随机float值
                     娱乐一下:
                                 select FLOOR(RAND()*10)
                                 select FLOOR(RAND()*10)
                                 select FLOOR(RAND()*10)
                              说明:一次运行上面三个语句,你会得到什么?没错,0-999的数字,嘿,能不能联想什么呀,这就是福利彩票的 3D,体育彩票的 排列三  ,排列五,;排列七呢?彩票是福利,不要赌博啊。
           Floor:返回小于等于所给数字表达式的最大整数
                     格式:floor()
                     实例:select floor(rand()*10)
                     说明:返回0-9随机数字
              Ceiling:返回大于等于所给数字表达式的最大整数
                     格式:ceiling()
                     实例:select ceiling(22.5678)
                     说明:返回值为:23
              Round:四舍五入:
                     格式:round(num,精度)
                     实例:select round(22.5678)
                     说明:返回值为:22.5700
              Sqrt:平方根:
                     格式:sqrt()
                     实例:select sqrt(4)
                     说明:返回值为2
字符串函数:left、right、len、lower、upper、ltrim、rtrim、replace、reverse、substring
              Left
                     格式:left(‘字符串’,’个数’)
                     实例:select left(‘abc’,2)
                     说明:返回值:ab
              Right
                     格式:right(‘字符串’,’个数’)
                     实例:select fight(‘abc’,2)
                     说明:返回值:bc
              Len
                     格式:len(‘字符串’,’个数’)
                     实例:select len(‘abc’,2)
                     说明:返回值:3,计算字符串长度
              Lower
                     格式:lower()
                     实例:select lower(‘ABCDEF’)
                     说明:返回值为:abcdef,大写转换为小写
              Upper
                     格式:upper()
                     实例:select upper(‘abcdef’)
                     说明:返回值为:ABCDEF,小写转换为大写
              Ltrim
                     格式:ltrim()
                     实例:select ltrim(‘     abc’)
                     说明:去除左空格,返回值为:“abc”
              Rtrim
                     格式:rtrim()
                     实例:select rtrim(‘abc      ’)
                     说明:去除右空格,返回值为:“abc”
              Replace
                     格式:replace(‘原字符串’,’查找串’,’替换串’)
                     实例:select replace(‘abcdef’,’cd’,’dc’)
                     说明:替换,返回值为:“abdcef”
              Reverse
                     格式:reverse()
                     实例:select reverse(‘abc’)
                     说明:返回值为:cba,颠倒、倒叙输出
              Substring
                     格式:substring(‘原串’,’起始位置’,’个数’)
                     实例:select substring(‘abc123def’,4,3)
                     说明:返回值为:123,截串。
常用系统函数:
              Current_user:返回当前用户名
                     实例:select current_user
                     说明:返回当前用户名
              @@error:错误代码:
                     实例:select @@error
                     说明:返回值为0时,说明执行语句成功
              @@powcount:返回上一行执行的行数:
                     实例:select @@powcount
                     说明:返回值为上一次执行的行数

你可能感兴趣的:(sql,数据库,职场,休闲)