【2017-03-13】数学函数、字符串函数、转换函数、时间日期函数、表连接

一、数学函数(针对值类型进行的操作)

1、ceiling():取上限

将小数点位进上,不管小数点位大小。

2、floor():取下限

将小数点位舍去,不管小数点位大小

3、round( , ):四舍五入,括号内第二个值为保存到小数点后几位

4、abs():绝对值

5、pi():3.1415926

 

6、sqrt --开根号

7、square --平方根

注:select --映射,把结果映射到结果框中

select 10 

print --打印,把结果打印在消息框中

print 20

二、字符串函数

1、UPPER():转化为大写

pic是一个列

2、LOWER():转化为小写

3、LTRIM():去左边空格

4、RTRIM():去右边空格

5、REPLACE(,,):替换 -- 括号内第一个值为要操作的字符串,第二个值为要被替换掉的字符,第三个为要换为的字符

--去掉所有空格,用替换实现

6、SUBSTRING (,,):字符串截取。--(要操作的字符串,开始截取的索引从1开始,截取位数)

7、LEFT(,):从左边截取。--(要操作的字符串,截取位数)

8、RIGHT(,):从右边截取。--(要操作的字符串,截取位数)

 

9、LEN():字符串的长度。

 三、转换函数

select 列名+列名 from 表名 :两列都为值类型的话,返回的是两列对应值相加;若两列为字符串类型的话为两列对应字符串拼接。

1、CONVERT(a,b): 类型转换。--(要转换为的类型,要转换的列名)

2、cast(a as b):作用一样--(要转换的列 as 要转换为的类型)

 

四、时间日期函数

1、GETDATE() :获取当前时间日期

2、YEAR():获取年份

3、MONTH():获取月份

4、DAY():获取日期

 5、ISDATE():判断时间日期格式是否正确,返回的是bit类型。

6、DATEADD(a,b,c):在日期上增加。(天/月/年,数量,在哪个日期上增加)

7、DATENAME(a,b):

 -- 当前日期是星期几

--在当前日期上加9天是星期几

--该日期是此年的第几周

--当前日期是次年的第几天

五、表连接

select * from Student ,Score where Stuent.Sno=Score.Sno      ---查询出来的数据的行数为外键表数据的行数

 1、select Student.Sno,Sname,Cno,Degree from Student,Score where Student.Sno=Score.Sno

 2、select Student.Sno,Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno 

 3、select Student.Sno,Sname,Cno,Degree from Student left join Scroe on Student.Sno=Scroe.Sno 

 4、select Student.Sno,Sname,Cno,Degree from Student right join Scroe on Student.Sno=Scroe.Sno 

 其中1和2返回的功能一样,推荐第一种。

3和4 在 join 前加上left 是以 join左边的表为主

        在 join前加上right 是以 join 右边的表为主

 纵连接:如果两个表列数一样,每列对应的数据类型一样,才可以进行纵连接。

select Sno,Sname,Ssex,Sbirthday from Student union select Tno,Tname,Tsex,Tbirthday from Teacher

例题:

查询所有学生的Sname、Cname和Degree列。三句代码都可以实现功能,推荐第二种

select (select Sname from Student where Student.Sno=Score.Sno),(select Cname from Course where Course.Cno=Score.Cno),Degree from Score
select Sname,Cname,DEGREE from Student,Course,Score where Student.Sno=Score.Sno and Course.Cno=Score.Cno
select Sname,Cname,DEGREE from Student join Score on Student.Sno=Score.Sno join Course on Course.Cno=Score.Cno

原文地址:https://www.cnblogs.com/qq609113043/p/6543554.html