常用的函数

--数学函数

select 列 ,sqrt(列) from score --开平方,平方根

select 数列, ceiling(数列) from car -- 取大于当前小数的最小整数

select 数列, floor(数列) from car -- 取小于当前小数的最大整数

select 数列, round(数列,-1) from car -- 四舍五入,保留几位,-1保留10位

select ABS(-2) -- 取绝对值

select RAND()*100 -- 生成随机数,默认是生成0-1之间的数

--字符串函数

select LOWER('Welcome') --转小写 select UPPER('welcome') --转大写 select LTRIM('   hello')--压缩左边的空格 select RTRIM('hello   ')--压缩右边的空格 select * from News select left(title,5) from News -- 从左侧截取字符串,截5个字符 select right(title,5) from News --从右侧截取字符串,截5个字符 select substring(title,3,5) from News--从某位置截取字符串,截取长度为n select reverse(title) from News -- 翻转字符串 select replace(title,'中国','美国') from News --替换字符串 select STUFF(title,4,3,'中国') from News--替换指定位置的字符串 从第四个位置换了3三个字符 select LEN(' hello') -- 返回字符串的长度

--日期时间函数

select * from student select year(sbirthday) from student -- 取日期时间中的年份 select month(sbirthday) from student -- 取日期时间中的月份 select day(sbirthday) from student -- 取日期时间中的天 select GETDATE() --获取当前系统的时间 select datepart(yy,sbirthday) from student--取日期时间的一部分

--类型转换 select len(cast(0.2 as varchar(50)))--cast(源数据 as 目标类型)--将0.2转换为字符串 len代表字符串的长度

原文地址:https://www.cnblogs.com/hansonglin/p/4662924.html