-----------------类型转换函数-----------------------------------
cast(表达式 as 数据类型)
convert(数据类型,表达式)
1.select 100.0 + cast('1000' as int) //结果为:1000.0
2.select 100.0 + convert(int,'1000') //结果为:1000.0


---使用union联合结果集
--使用union和union all都能进行联合,区别在于:union会自动去除重复并重新排列数据,而union all不会。
select tname,tage from tperson
union all/union
select fname,fage from fperson
--联合时一定要列数相同,数据类型至少可以转换

------------------------------------------------------------------------------------
--从Myorders表中统计每种商品的销售总价,并在底部汇总
select
商品名称,
销售总价 = sum(销售数量*销售单价)
from Myorders
group by 商品名称
union all
select '总销售价格',sum(销售数量*销售单价) from Myorders
order by 销售总价 asc

---------向表中插入多条记录--------------------------------------------------------
0.把现有表插到新表(新表不能存在),会自动建新表
1.select * into 目标表 from 数据来源表 //原表中的约束并不会被一起带到目标表中
1.1.怎样只取表结构? select top 0 * into 目标表 from 数据来源表
-------------------------------------------------------------------------------------------
------------------------常用的字符串函数----------------------------------------------
1.len() --计算字符的个数
--补充:datalength() 返回所占用字节的个数,这个不是字符串函数
2.upper() --将字母全转换为大写
lower() --将字母全转换为小写
3.ltrim() --去掉字符串左边空格
rtrim() --去掉字符串右边空格
4.left(,num1) --从左边截取num1个字符
right(,num2) --从右边截取num2个字符
substring(,num1,num2) --从num1位置的字符开始,截取num2个字符(包含第num1个字符)
-------------------------------------------------------------------------------------------
--------------日期函数-----------------
1.getdate() --当前日期

2.dateadd(depart,add,date) --计算增加后的日期
dateadd(year,2,getdate()) --从当前日期增加两年

3. datediff() --计算两个日期差
datediff(year,2008-12-12,getdate()) --计算从现在到2008-12-12之间的年份
datediff(month,2008-12-12,getdate()) --计算从现在到2008-12-12之间有多少个月

4.datepart() --获取日期的某部分的值
datepart(year,getdate()) year(getdate()) --当前日期的年部分
datepart(month,getdate()) month(getdate()) --当前日期的月部分

--不同年份入职的员工个数
select
入职年份 = year(jointime),
count * as 人数
from 表
group by year(jointime)

posted on 2019-08-08 15:47  漂乎兮乎  阅读(184)  评论(0编辑  收藏  举报