2017-3-13 sqlserver数学函数 字符串函数 转换函数 表连接

(一)数学函数

1.ceiling()   舍去小数点后面的,直接加一

select ceiling(10.4) ----  11

2.floor()   舍去小数后面的

select floor(10.4)  ----- 10

3.round()   四舍五入

select round(10.4)   ----- 10

select ROUND(PI(),4) ---  四舍五入保留小数点后4位

4.abs()  ---   绝对值

select ABS(-1)

5.pi() ---- 表述圆周率

6.sqrt() ---- 开根号

select SQRT(9)  ---- 3

7.square()----- 平方根

select SQUARE(3)  ---- 9

(二)字符串函数

1.upper() ---- 转换成大写

select UPPER('adsabdjkash')    ---  都转换成大写

 lower()  ----  转换成小写

select UPPER('SADNSAh')  ---- 换成小写

2.ltrim   --- 去掉左空格

   rtrim ----- 去掉右空格

3.replace(3个参数)  ---- 替换

select REPLACE('abcdefg','abc','哈哈')  ----(要操作的字符串,要替换的,替换的内容)

4.substring(截取的字符串,下标,长度)  ----- 字符串截取

select SUBSTRING('abcdef',1,2)   ----  注意截取的字符串下标从1开始

left(要截取的字符串,长度)----- 从左边截取

right(要截取的字符串,长度) ------ 从右边截取

5.len(字符串) ---- 获取字符串的长度

select LEN('aaaaaa')

6.字符串的相加

如果2列都是数字则进行数学运算,如果是字符串则进行并接

(三)转换函数

1.convernet(要转换成的数据,数据)

2.cast(数据 as 要转换成的类型)

(四)时间日期函数

1.getdate()  ---- 获取系统当前时间

select GETDATE()

year(getdate())  ---- 获取年份     month(getdate()) ---- 获取月份         day(getdate())  ---- 获取日期

select YEAR(GETDATE())

2.isdate()  ---  判断日期是否正确   返回bit类型  

select ISDATE('2017-3-33')  ---  返回0,错误

3.dateadd(要加入年/月/日,加入的年/月/日数,相加的时间)

select DATEADD(MONTH,2,'2017-3-3')   ----- 在2017-3-3的时间上加上2个月

4.显示当前时间星期几  -------  datename(weekday,getdate())

select datename(weekday,getdate())

 显示当前时间这个月的几号  -------------select DATENAME(day,GETDATE())

显示当前日期是今年的第几天 ------------select DATENAME(DAYOFYEAR,GETDATE())

5.时间间隔

datediff(指定返回类型,时间1,时间2,)---时间2-时间1,返回指定的时间类型

 

select sname,DATEDIFF(YY,Sbirthday,GETDATE()) as 年龄 from Student 

系统时间减去生日,获取年龄

(四)表连接

1. 表连接 2中不同的方式   student 表  score表 

select * from Student,Score where Student.Sno=Score.Sno

select * from Student join Score on Student.Sno=Score.Sno

2.左连接,右连接 以左边或者右边的表位基础

select * from Student left join Score on Student.Sno=Score.Sno    ---以左边的表为基础进行链接 

select * from Student right join Score on Student.Sno=Score.Sno    ---以右边的表为基础进行链接 

3.多表链接  student 表  score表  course表  

select * from Student,Score,Course where Student.Sno=Score.Sno and Course.Cno=Score.Cno

select * from Student join Score on Student.Sno=Score.Sno join Course on Course.Cno=Score.Cno

(五)union

1.UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

注意:UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。

 

 

 

 

 

 

posted @ 2017-03-13 15:52  青年a  阅读(808)  评论(0编辑  收藏  举报