SQL中函数的种类:
聚合函数、数学函数、字符串函数、时间函数、元数据函数
聚合函数:
sum():求和函数
select SUM(Admin.age) as Age from Admin
max():求最大值
select Max(Admin.age) as Maxage from Admin
min():求最小值
select Min(Admin.age) as Minage from Admin
count():求记录数
select Count(Admin.age) as CountAge from Admin where age>24
avg():求平均值
select Avg(Admin.age) as AvgAge from Admin
distinct():去掉重复项,保留不重复的记录
select Distinct Admin.age as AvgAge from Admin
数字函数:
abs():取绝对值
select Abs(Admin.age) as AbsAge from Admin
pi():取圆周率
select PI() as PiValue from Admin
power(3,4):进行幂运算,3的4次方
select POWER(3,4) as Number from Admin
ceiling(1.1212):向上取整,值为2
select Ceiling(1.7980)
Floor(1.9898):向下取整,值为1
select Floor(1.9898)
rand():随机取数,随机从0-1之前取一个数
select RAND()
round(1.56115,2):四舍五入,2为小数舍的位数
select Round(1.56115,2)
square(5):返回平方
select SQUARE(5) //返回值为25
sqrt(9):返回平方根
select Sqrt(9) //返回值为 3
字符串函数:
ascii():取字符串的ASCII编码
charindex('abc','dasdasdabcads'):从第二个参数中,搜索第一个参数,如果不存在返回0,如果存在返回出现的位置
select CHARINDEX('abc','this is first abc') //返回值为 15
left('name',2):保留字符串从左边开始的几个字符
select LEFT('name',2) //返回值为 'na'
rigth('name',2):保留字符串从右边开始的几个字符
select Right('name',2) //返回值为 'me'
len():返回字符串的长度
select LEN('21312141') //返回值为 8
replace('name','a','o'):字符串替换,将第二个参数替换为第三个参数
select REPLACE('this my name','my','you') //返回值为 'this you name'
reverse():字符串左右颠倒
select REVERSE('this is name') //返回值为 'eman si siht'
substring('name',2,3) :截取字符串,第二个参数是起始位置,第三个参数是截取字符的长度
select SUBSTRING('this is new name',3,7) //返回值为 'is is n'
日期函数:
getdate()
day()
month()
year()
datediff()
dateadd('day',1,getdate())
转换函数:
cast():将第一个参数转化为第二个参数类型
select CAST('12.2' as decimal) //返回值为 12
convert():将日期函数转化为需要类型
select CONVERT(varchar(10),getdate(),120) //返回值为 2017-08-02
元数据函数:
col_length()
col_name()
db_name()
浙公网安备 33010602011771号