我想获取日期和时间大于给定日期的行。这是我的表:
EvDate EvTime ClBdg Name Event
18.01.2015 10:55:01 001 Jane enter
18.01.2015 19:31:21 003 Brad exit
19.01.2015 13:31:21 002 Lucy exit
鉴于date
为18.01.2015
,time
为17:00:00
。期望的值是:
EvDate EvTime ClBdg Name Event
18.01.2015 19:31:21 003 Brad exit
19.01.2015 13:31:21 002 Lucy exit
任何人都可以帮助我吗?
编辑: 为了更清楚,我编辑了我的帖子。以下是执行查询的java代码:
private static final SimpleDateFormat sdfTime=new SimpleDateFormat("HH:mm:ss");
private static final SimpleDateFormat sdfDate=new SimpleDateFormat("dd.MM.yyyy");
private long givenTimeStamp = 1421600400L;
public static Date givenDate = new Date(givenTimeStamp);
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String DATABASE = "jdbc:ucanaccess://"+CONF.getString("db.location")+";jackcessOpener=uz.lexus.access.crypto.MyCryptoProvider";
connection= DriverManager.getConnection(DATABASE, null, null);
List<Event> events=new ArrayList<Event>();
String selectEvents = "select EvDate,EvTime,ClBdg,Event from Evntlog_tbl where Event in ('enter','exit') and not ClBdg=0 and EvDate >= ? and EvTime > ? order by EvDate desc, EvTime desc";
PreparedStatement prstm= connection.prepareStatement(selectEvent);
java.sql.Date date=new java.sql.Date(sdfDate.parse(sdfDate.format()).getTime(givenDate));
java.sql.Time time=new Time(sdfTime.parse(sdfTime.format(givenDate)).getTime());
prstm.setDate(1,date);
prstm.setTime(2,time);
ResultSet rs = prstm.executeQuery();
现在的问题是:selectEvent中的查询是否正确?
答案 0 :(得分:4)
试试这个,
由于您有Evdate
和EvTime
SELECT * FROM table_name WHERE EvDate >'18.01.2015' OR (EvDate = '18.01.2015' AND EvTime > '17:00:00'
<强>更新强> -
附加fiddle会很有帮助。
答案 1 :(得分:3)
SELECT *
FROM table1
WHERE EvDate + EvTime > '2015-01-18T17:00:00'
相反,您可以考虑创建一个添加日期和时间的PERSISTED计算列。持久计算列将允许索引
ALTER TABLE table1 ADD cEVdatetime AS EvDate + EvTime PERSISTED
答案 2 :(得分:0)
这可能有助于你
declare @t table(a date,b time)
insert into @t values (getdate(),'7:00:00 AM')
select * from @t
where a>'2015/01/19' or (a=getdate() and a>'6:00:00 AM')
答案 3 :(得分:0)
我假设EvDate数据类型是Date,EvTime数据类型是Time。 试试这个......
选择 *来自yourtablename 其中CONVERT(VARCHAR(20),EvDate,112)&gt; = 20150118 AND EvTime&gt; ='17:00:00'