函数

select * from News

//数学函数

select SQRT(2) //取平方根
select ABS(-234) //取绝对值

select floor( COUNT(*)/5.0 )from News //取小于这个小数的最大整数
select ceiling ( COUNT(*)/5.0 )from News //取大于这个小数的最小整数
select ROUND(4.523,0) //保留小数点后几位


//字符串函数

select LOWER('UserName') //转小写
select UPPER('UserName') //转大写

select LTRIM(' me') //压缩左边的空格
select RTRIM('me ') //压缩右边的空格

select LEFT(Title,5) from News //从左边截取几个字符
select RIGHT(Title,5) from News //从右边截取几个字符
select SUBSTRING(Title,3,5) from News //从哪个位置开始截,截几个

select REVERSE(Title) from News //翻转字符串
select CHARINDEX('中国',Title) from News //字符串出现的位置
select REPLACE(Title,'中国','美国') from News //替换字符串
select STUFF(Title,3,4,'china') from News //指定位置,指定个数,替换字符串

//其它函数
select RAND() //随机数生成函数 0-1之间
select LEN('china') //取字符串长度
select GETDATE() //获取当前时间
select YEAR(sbirthday) from Student //取日期时间的年份
select MONTH(sbirthday)from student //取日期时间的月份
select DAY(sbirthday) from student //取日期时间的天数

select DATEPART(ss,sbirthday)from student //取日期时间的部分yy年mm月dd日hh小时mi分钟ss秒
select CAST('123' as int) //转换格式


--聚合函数 主要作用是对结果集进行聚和计算
--sum 求和
select sum(Oil) 总和 ,AVG(Oil) as 平均 from Car where Name like '%奥迪%'
--avg 求平均
select AVG(oil) 平均油耗 from Car where Name like '%奥迪%'
--max
--同上
--min
--同上
--count 个数
select COUNT(*) from Car

--分组 group by
--按系列进行分组,显示系列
select Brand from Car group by Brand
--先筛选宝马再按油耗小于8的降序排序显示出来
select Brand,AVG(oil) from Car where Name like '%宝马%'
group by Brand having AVG(Oil)<8 order by AVG(oil) desc
--去重
select distinct Brand from Car

原文地址:https://www.cnblogs.com/languang/p/4547045.html