聚合函数、数学函数、日期时间函数

ggregate Functions(Transact-SQL)聚合函数


AVG:求平均分
COUNT:计算个数
MAX:求最大值
MIN:求最小值
SUM:求和

求平均身高
select AVG (shengao) from student
as:添加的列名
select AVG (shengao) as 平均身高 from student

求个数
select COUNT (*) from student where xingbie = '男'

求最大
select MAX(shengao) from student

求最小值
select MIN(shengao) from student

求和
select SUM(shengao) from student


结合分组使用聚合函数,分别对每组进行计算显示
having:筛选人数大于6个
select COUNT(*), avg(shengao) class  from xueshengxinxibiao group by class
having count(*)>6

数学函数
ABS:取绝对值
select ABS(-1)

CEILING:天花板,小数点后只要有值不是零,都会进一取整
select CEILING(1.0)
select CEILING(1.1)

FLOOR:小数点前面取整数,和上面反着
select FLOOR(1.9)
select FLOOR(2.0)

PI:圆周率
select PI()

round:4舍5入,后边还有个参数,标识到小树点后面几位
select round(3.5437,2)
select round(3.5474,2)

rand:随机的意思
select rand()

sqrt:求平方根
select sqrt(16)

square:求平方
select square(4)

print:输出的意思,输出到消息框里面

日期时间函数
select ‘1990-09-09’

dateadd:往一个日期时间里面,去添加一定的时间

加一年
select dateadd(year,1,‘1990-09-09’)
加一月
select dateadd(month,1,‘2014-03-31’)

datediff:取时间差
差几年
select datediff(year,‘2011-09-22’,‘2014-01-01’)
差几月
select datediff(month,‘2011-09-22’,‘2014-01-01’)
差几天
select datediff(day,‘2011-09-22’,‘2014-01-01’)
差几个星期
select datediff(week,‘2011-09-22’,‘2014-01-01’)

datename:单独返回日期时间中的年月日时分秒,或者星期,第几周
select datename(year,‘2014-11-25’)
select datename(week,‘2014-11-25’)【显示为这一年的第48个周】
select datename(weekday,‘2014-11-25’)【显示星期二】

datepart:取指定段的整数
select datepart(year,‘2014-11-25’)【显示2014】
select datepart(week,‘2014-11-25’)【显示48】
select datepart(weekday,‘2014-11-25’)【显示3】

day:返回月份多少天
select day(‘2014-11-25’)【显示25】

getdate:返回数据库服务时间
select getdate()

isdate:判断一个日期时间是否正确,正确返回一,不正确返回零
select isdate(‘2014-02-12’)

month:获取月份
select month(‘2014-08-22’)
year:获取年份
select year(‘2014-08-22’)

sysdatetime:获取电脑的系统时间
select sysdatetime()

原文地址:https://www.cnblogs.com/liuyuwen900326/p/4125761.html