如何获得两次之间的记录 [英] How To get the records between two times

查看:98
本文介绍了如何获得两次之间的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我有一张桌子和列是从时间(日期时间)和全时(日期时间)等...



如何获得两个日期和时间之间的记录。我的

这样查询



 选择 * 来自 tblEmployeeTimeCards 其中 
((FromTime 之间 转换 datetime ' From-Time' 103 转换 datetime ' < span class =code-string> ToTime', 103 ))(ToTime 之间 转换 datetime ' FormTime' 103 转换 datetime ' Totime' 103 )))





但不是完美无瑕



请给我任何想法



感谢你

解决方案

试试这个:

  SELECT  *  FROM  tblEmployeeTimeCards 
WHERE
CONVERT DATETIME ,dtcreated)> = fromdate AND
CONVERT DATETIME ,dtcreated)< = todate


如果您为数据库中的相关列包含DDL,则可以提供更好的帮助表格,数据库表格中的真实数据样本以及FormTime和Totime的真实样本。另外,你没有说出问题所在。 不能正常工作可能意味着语法错误或返回错误的行或谁知道什么?



我的下面的示例在查询字符串中使用SQL参数而不是文字。更好的性能并防止SQL注入攻击。



以下两个示例是等效的。一个使用BETWEEN运算符,一个不使用。



使用FromTime> = fromdatetime-parameter和FromTime< = todatetime-parameter
$ b $的行b OR

ToTime> = fromdatetime-parameter和ToTime< = todatetime-parameter



 < span class =code-keyword> Dim  cn  As  SqlConnection =  SqlConnection(connectionString) )
Dim obCommand As SqlCommand = New SqlCommand( select * from tblEmployeeTimeCards& _
其中& _
(FromTime BETWEEN @FromDateTime AND @ToDateTime) & _
(ToTime BETWEEN @FromDateTime和@ToDateTime);
,cn)
obCommand.Parameters.AddWithValue( FromDateTime CDate 2013-03-01 08:00:00 AM))
obCommand.Parameters.AddWithValue( < span class =code-string> ToDateTime, CDate 2013-03-01 10:59:59 AM))
Dim rs As SqlDataReader = obCommand.ExecuteReader







  Dim  cn  As  SqlConnection =  SqlConnection(connectionString)
Dim obCommand A s SqlCommand = SqlCommand( 选择*来自tblEmployeeTimeCards& _
其中& _
(FromTime> = @ FromDateTime AND FromTime< = @ ToDateTime) & _
(ToTime> = @ FromDateTime AND FromTime< = @ ToDateTime);
,cn )
obCommand.Parameters.AddWithValue( FromDateTime CDate 2013-03-01 08:00:00 AM ))
obCommand.Parameters.AddWithValue( ToDateTime CDate 2013-03-01 10:59:59 AM))
Dim rs As SqlDataReader = obCommand.ExecuteReader


(((FromTime BETWEEN DATEADD(hh,DATEDIF) F(hh, 0 '  2013-04-21 07:30:00'),0)和 - '2013-04-21 07:30:00' 
DATEADD(hh,DATEDIFF(hh, 0 ' 2013-04-21 13:00:00'),0)) - @To

(FromTime< = DATEADD(hh,DATEDIFF(hh, 0 ' 2013-04-21 07:30:00'),0)和 - -From
ToTime> = DATEADD(hh,DATEDIFF(hh, 0 ' 2013-04-21 07:30:00'),0)) - 来自


(ToTime BETWEEN DATEADD(hh,DATEDIFF(hh, 0 ' 2013-04-21 07:30:00'),0)AND - 来自
DATEADD(hh,DATEDI FF(hh, 0 ' 2013-04-21 11:30:00'),0) - 到

(FromTime< = DATEADD(hh,DATEDIFF) (hh, 0 ' 2013-04-21 13 :00:00'),0) - 至少
ToTime> = DATEADD(hh,DATEDIFF(hh, 0 ' 2013-04-21 13:00:00') ,0)) - 到
))

AND
(((FromTime BeTween DATEADD) (MINUTE,DATEDIFF(MINUTE, 0 ' 2013- 04-21 07:30:00'),0)和 - 来自
DATEADD(MINUTE,DATEDIFF(MINUTE, 0 ,< span class =code-comment>' 2013-04-21 13:00:00' ),0) -

FromTime< = DATEADD(MINUTE,DATEDIFF(MINUTE, 0 2013-04-21 07:30:00'), 0)和 - 来自
ToTime> = DATEADD(MINUTE,DATEDIFF(MINUTE, 0 ' 2013-04-21 07:30:00'),0) - 来自


(DATEADD之间的ToTime(MINUTE,DATEDIFF(MINUTE, 0 ' 2013-04-21 07:30:00'),0)和--from
DATEADD(MINUTE, DATEDIFF(MINUTE, 0 ' 2013-04-21 13:00:00'),0) - TO

FromTime< = DATEADD(MINUTE,DATEDIFF( MINUTE, 0 ' 2013-04-21 13:00:00'),0)和--To
ToTime> = DATEADD(MINUTE,DATEDIFF(MINUTE, 0 ' 2013-04-21 13:00:00'),0)--To
)))
ETC.EmployeeID = 71 ETC.ClientID = 20
订购ETC.TCID desc


Hi Friends,

I have one table and columns are fromtime(datetime) and totime(datetime) and etc...

how to get the records between two date&time. my
query like this

select * from tblEmployeeTimeCards where
 ((FromTime between Convert(datetime,'From-Time',103) and Convert(datetime,'ToTime',103)) or (ToTime between Convert(datetime,'FormTime',103) and Convert(datetime,'Totime',103)))



but not wroking perfectly

please give me any ideas

thanking you

解决方案

Try this:

SELECT * FROM tblEmployeeTimeCards 
WHERE 
CONVERT(DATETIME, dtcreated) >= fromdate AND 
CONVERT(DATETIME, dtcreated)<= todate


Better help could be provided if you included DDL for pertinent columns in the database tables, real samples of data in the database table and real samples of ''FormTime'' and ''Totime''. Also, you did not say what the problem was. "Not working perfectly" could mean a syntax error or the wrong rows are returned or who knows what?

My examples below use SQL Parameters instead of literals in the query string. Better performance and prevents SQL Injection attacks.

The two examples below are equivalent. One uses the BETWEEN operator and one does not.

Rows with FromTime >= fromdatetime-parameter and FromTime <= todatetime-parameter
OR
ToTime >= fromdatetime-parameter and ToTime <= todatetime-parameter

Dim cn As SqlConnection = New SqlConnection(connectionString)
Dim obCommand As SqlCommand = New SqlCommand("select * from tblEmployeeTimeCards " & _
"Where " & _
(FromTime BETWEEN @FromDateTime AND @ToDateTime) OR " & _
(ToTime BETWEEN @FromDateTime AND @ToDateTime) ;", cn)
obCommand.Parameters.AddWithValue("FromDateTime", CDate("2013-03-01 08:00:00 AM"))
obCommand.Parameters.AddWithValue("ToDateTime", CDate("2013-03-01 10:59:59 AM"))
Dim rs As SqlDataReader = obCommand.ExecuteReader




Dim cn As SqlConnection = New SqlConnection(connectionString)
Dim obCommand As SqlCommand = New SqlCommand("select * from tblEmployeeTimeCards " & _
"Where " & _
(FromTime>=@FromDateTime AND FromTime<=@ToDateTime) OR " & _
(ToTime>=@FromDateTime AND FromTime<=@ToDateTime) ;", cn)
obCommand.Parameters.AddWithValue("FromDateTime", CDate("2013-03-01 08:00:00 AM"))
obCommand.Parameters.AddWithValue("ToDateTime", CDate("2013-03-01 10:59:59 AM"))
Dim rs As SqlDataReader = obCommand.ExecuteReader


(((FromTime BETWEEN DATEADD(hh, DATEDIFF(hh, 0,'2013-04-21 07:30:00'), 0)and  --'2013-04-21 07:30:00'
 DATEADD(hh, DATEDIFF(hh, 0,'2013-04-21 13:00:00'), 0))--@To
or
(FromTime <= DATEADD(hh, DATEDIFF(hh, 0,'2013-04-21 07:30:00' ), 0)and  --From
ToTime >= DATEADD(hh, DATEDIFF(hh, 0,'2013-04-21 07:30:00' ), 0)) --From
  )
or
(ToTime BETWEEN DATEADD(hh, DATEDIFF(hh, 0, '2013-04-21 07:30:00'), 0)AND --From
 DATEADD(hh, DATEDIFF(hh, 0, '2013-04-21 11:30:00'), 0) --To
or
(FromTime <= DATEADD(hh, DATEDIFF(hh, 0,'2013-04-21 13:00:00'), 0) --To
 and ToTime >= DATEADD(hh, DATEDIFF(hh, 0, '2013-04-21 13:00:00'), 0)) --To
))

 AND
 (( (FromTime BeTween DATEADD(MINUTE, DATEDIFF(MINUTE, 0,'2013-04-21 07:30:00' ), 0)and  --From
    DATEADD(MINUTE, DATEDIFF(MINUTE, 0,'2013-04-21 13:00:00' ), 0)--To
    or
     FromTime <= DATEADD(MINUTE, DATEDIFF(MINUTE, 0,'2013-04-21 07:30:00' ), 0)and  --From
   ToTime >= DATEADD(MINUTE, DATEDIFF(MINUTE, 0,'2013-04-21 07:30:00' ), 0) --From
    )
 or
  (ToTime between DATEADD(MINUTE, DATEDIFF(MINUTE, 0,'2013-04-21 07:30:00'), 0)and  --from
 DATEADD(MINUTE, DATEDIFF(MINUTE, 0,'2013-04-21 13:00:00'), 0)--TO
 or
 FromTime <= DATEADD(MINUTE, DATEDIFF(MINUTE, 0,'2013-04-21 13:00:00'), 0)and  --To
ToTime >= DATEADD(MINUTE, DATEDIFF(MINUTE, 0,'2013-04-21 13:00:00'), 0) --To
 ) ))
   and ETC.EmployeeID=71 and ETC.ClientID=20
 order by ETC.TCID desc


这篇关于如何获得两次之间的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆