sql语句聚合等疑难问题收集

王朝学院·作者佚名  2016-05-20
窄屏简体版  字體: 小  |  中  |  大  |  超大  

sql语句聚合等疑难问题收集------------------------------------------------------------------------------------

除法运算

select 500 / (501) *100

结果为0,由于数值是INT类型,怎么才能得到小数点呢?

select 500 / (501 + 0.0) *100

99.800300这样就有小数点了。。。天那。。SQL真是有点笨。

------------------------------------------------------------------------------------

获取当前日期 昨天日期

select getdate()-1 --2015-06-03 13:33:14.957select DATEADD(DAY, -1, GETDATE()) --2015-06-03 13:33:14.957select convert(varchar(10),getdate()-1,120) --2015-06-03

Year(getdate()) --当前年Month(getdate()) --当前月Day(getdate()) --当前日Datediff(d,时间字段,getdate()) --得到离过生日还剩的天数其它格式转换的范例如下:--YYYY/MM/DDSelect Convert(varchar(10),Getdate(),111)--YYYYMMDDSelect Convert(varchar(10),Getdate(),112)--HH:MM:SSSelect Convert(varchar(8),Getdate(),108)--HH:MM:SS:mmmSelect Convert(varchar(12),Getdate(),114)

------------------------------------------------------------------------------------

数据表中比如有20条数据读取前5条数据select top5 * from 表 order by id desc读取前5条数据后的5条数据(就是查询前10条记录,取后面5个)select top 5 * from 表 whereid not in (select top 5 id from 表)

===================================================================================SELECT TOP 11 * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY StuID) AS RowNumber, * FROM StuInfo) tWHERE RowNumber >= 10 假设StuID是StuInfo表的主键“学号”。先按学号排序,生成行号,再返回行号>=10时的前11条记录,即行号为10~20的11条记录。=======================================================================select top 10 * from [table_name] where id in (select top 20 id from [table_name] order by id desc) order by id asc这样显示出来的就是:从11到20条数据

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航