就像查询日期时间一样

时间:2010-10-27 11:15:42

标签: sql sql-server sql-server-2005

这可能会像SQL Server 2005中的DateTime上的查询一样触发。

select cur.EvrIdent,
cur.VrdTarih,
cur.VrdModTim,
cur.VrdNo,
(select PmpKod from Bnz_Pompalar where PmpIdent=(select GunPmpId from Bnz_Tabancalar where GunIdent=TbdGunId)) as [Pump No],
(select GunKod from Bnz_Tabancalar where GunIdent=TbdGunId) as [Nozzle No],
(select StokKod from Stoklar where StokIdent=(select GunStkId from Bnz_Tabancalar where GunIdent=TbdGunId)) as [Fuel Type],
isnull((select tprv.TbdSon from Bnz_TabancaDetay tprv where tprv.TbdEvrIdent=(select top 1 prv.EvrIdent from Bnz_Vardiya prv where prv.EvrIdent < cur.EvrIdent order by prv.EvrIdent desc) and tprv.TbdGunId=t.TbdGunId),0) as [Accumulated Opening],
t.TbdSon as [Accumulated Final],
isnull((select tprv.TbdTotal from Bnz_TabancaDetay tprv where tprv.TbdEvrIdent=(select top 1 prv.EvrIdent from Bnz_Vardiya prv where prv.EvrIdent<cur.EvrIdent order by prv.EvrIdent desc) and tprv.TbdGunId=t.TbdGunId),0) as [Electronic Tot Opening],
t.TbdTotal as [Electronic Tot Final] 
from Bnz_TabancaDetay t 
join Bnz_Vardiya cur on (EvrIdent=TbdEvrIdent) 
--where cur.VrdModTim='" + date + "' 
order by cur.VrdTarih,cur.VrdNo,[Pump No],[Nozzle No]

根据这个问题我得到像这样的记录

1   2010-03-09 00:00:00.000 2010-03-11 05:57:31.247 1   01              101             TRB                     .0000   37.5600 .0000   428110.7700
1   2010-03-09 00:00:00.000 2010-03-11 05:57:31.247 1   01              201             HS                      .0000   332.5700    .0000   3708985.9100
1   2010-03-09 00:00:00.000 2010-03-11 05:57:31.247 1   02              102             TRB                     .0000   .0000   .0000   275481.1400
1   2010-03-09 00:00:00.000 2010-03-11 05:57:31.247 1   02              202             HS                      .0000   1041.9500   .0000   3496286.9500
1   2010-03-09 00:00:00.000 2010-03-11 05:57:31.247 1   03              103             UL                      .0000   .0000   .0000   346131.1600
2   2010-03-11 00:00:00.000 2010-03-12 05:59:02.937 1   01              201             HS                      332.5700    457.4500    3708985.9100    3710130.6100
2   2010-03-11 00:00:00.000 2010-03-12 05:59:02.937 1   02              102             TRB                     .0000   .0000   275481.1400 275627.0200
2   2010-03-11 00:00:00.000 2010-03-12 05:59:02.937 1   02              202             HS                      1041.9500   2471.9500   3496286.9500    3501872.4400

........... (more than 1000 rows)

但我已将cur.VrdModTim日期定为'2010-03-11 05:57:31.000'并且我想通过cur.VrdModTim(2010-03-11 05:57:31.000)过滤此数据,以便我只能获得5条记录。

2 个答案:

答案 0 :(得分:3)

当然是:

SELECT  1
WHERE   GETDATE() LIKE '%2010%'

但是,我相信,对于你的任务,会有更有效的方法。

你到底做了什么?

<强>更新

要查找给定秒内的所有记录,忽略毫秒,请使用:

SELECT  *
FROM    mytable
WHERE   mydatefield >= '2010-14-09 5:54:45'
        AND mydatefield < DATEADD(s, 1, '2010-14-09 5:54:45')

只要您在mydatefield上有索引,这将更有效率。

答案 1 :(得分:3)

我认为你最好使用BETWEEN operator

例如(来自MSDN):

SELECT BusinessEntityID
     , RateChangeDate

FROM HumanResources.EmployeePayHistory

WHERE RateChangeDate BETWEEN '20011212' AND '20020105'