数据库的类型转换,获取系统时间和联合

来源:互联网 发布:易哲软件 编辑:程序博客网 时间:2024/05/21 04:23

convert函数

select * from MyTest1001 order by cast(number as int) desc

select * from MyTest1001 order by convert(int,number) asc

获取系统时间

–两种获取时间的方式,但是输出结果不一样
select getdate() 是以列的方式显示的
print getdate() 输出结果是 06 14 2015 10:33PM

–对日期的转换,转换成各种国家格式的日期
select convert(varchar(20),getdate(),104)
style的格式,查sql帮助, (输入convert函数查询)

联合 把多个结果集联合成一个

select getdate()
print getdate()

–当使用union或者union all时必须保证多个结果集中列的数目一致,并且对应的数据类型一致(至少要兼容)
select ‘黄林’,18,’huanglin@qq.com’
union all –把行联合在一起
select ‘许正龙’,19,’xuzhenglong@qq.com’

–可以使用from语句将不同表中的数据联合在一起
–union all在联合的时候不会去除重复的数据,union在联合的时候会自动去除重复

–union all一般出现在最下面的求和
select
商品名称,
销售价格=(sum(销售数量*销售价格))
from MyOrders
group by 商品名称
union all
select ‘所有商品销售总价:’,SUM(销售数量*销售价格)from MyOrders

–将查询结果分为三行
select max(tsAge),min(tsAge),avg(tsAge) from TblStudent

select max(tsAge) from TblStudent
UNION ALL
select MIN(tsAge) from TblStudent
union all
select AVG(tsAge) from TblStudent

0 0