易截截图软件、单文件、免安装、纯绿色、仅160KB

SQL DATETIME字段比较

比如有个表其中有3个datetime字段,一般来说其中有个datetime字段的值会比其他2个大(修改后日期),那怎么找出这条记录?
來數據說明吧
...........

select * from TB where dt1>dt2 and dt1>dt3
union all
select * from TB where dt2>dt1 and dt2>dt3
union all
select * from TB where dt3>dt1 and dt3>dt1

比较两个日期的差:
用 datediff()

SQL code:
select * from TB where dt1>dt2 and dt1>dt3
union all
select * from TB where dt2>dt1 and dt2>dt3
union all
select * from TB where dt3>dt1 and dt3>dt1


找出记录还是找出字段?


DATEDIFF ( datepart , startdate , enddate )

SQL code:
create table #test
(dt1 datetime,
dt2 datetime,
dt3 datetime)

insert #test
select '20100110','20091221','20121212' union all
select '20091221','20121212','20100110' union all
select '20121212','20091221','20100110'

select
case when dt1>dt2 and dt1>dt3 then dt1 else null end,
case when dt2>dt1 and dt2>dt3 then dt2 else null end,
case when dt3>dt2 and dt3>dt1 then dt3 else null end
from #test



相关问答:

求一sql语句 - MS-SQL Server / 疑难问题

现在有两张表:文章主表A(articleId,articleTitle),文章评论表B(commentId,articleId,commentTitle)
现在我想实现这样的功能:列出文章列表,其中每篇文章标题下面列出此文章的前2个文章评论,请问sql语句怎么写啊 ......

求一sql语句 - MS-SQL Server / 基础类


id bh  
1 10
2 11
3 12 
4 15
5 16
6 22
7 25
8 26
9 27
10 28
将bh按连续分段出来返回字符串:
10~12,15~16,22,25~28
SQL code:


declare @t tabl ......

MS SQL作业问题 - MS-SQL Server / 应用实例

如何在SQL2005中设定定时作业,比如说定时清理某些表的数据,
或者是定时的将某些表的数据导出excel!
在线等待,急急急,最好是详细步骤!
之前我做的作业有点问题!
帮UP

参考:http://hi.baidu.com/toiota ......

求一SQL - MS-SQL Server / 基础类

tab1 字段:billdate,goodsid,incount,inmoney,outcount,outmoney,endprice,endcount,endamt
tab2 字段:goodsid,goodskind(商品类型)
tab3 字段:goodskind(商品类型),kindname
结果:
得到商品类型在一段时间 ......

两句SQL并一句,有问题. - MS-SQL Server / 基础类

第一句:
select ht.gfdm,gfdm.gfmc,gfdm.lb ,sum(jh.htdj*jh.htsl)as je
from ht left join gfdm on ht.gfdm=gfdm.gfdm 
 join jh on ht.htbh=jh.hth 
and ht.htbh not like'del%' and ht. ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号