sql 查询按日期和时间获取数据
sql query to get data by date and time
我想获取日期和时间大于给定日期的行。这是我的 table:
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
谁能帮帮我?
编辑:
为了更清楚,我编辑了我的 post。这是执行查询的 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();
Now the question is: Is the query in the selectEvent correct?
试试这个,
由于 Evdate
和 EvTime
有两个不同的列
SELECT * FROM table_name WHERE EvDate >'18.01.2015' OR (EvDate = '18.01.2015' AND EvTime > '17:00:00'
更新-
附加一个 fiddle ,会有帮助。
这可能对您有所帮助
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')
SELECT *
FROM table1
WHERE EvDate + EvTime > '2015-01-18T17:00:00'
相反,您可以考虑创建一个添加日期和时间的 PERSISTED 计算列。持久计算列将允许索引
ALTER TABLE table1 ADD cEVdatetime AS EvDate + EvTime PERSISTED
我假设 EvDate 数据类型是 Date 并且 EvTime 数据类型是 Time 。
试试这个....
select
* 来自你的表名
其中 CONVERT(VARCHAR(20),EvDate ,112)>=20150118 AND EvTime >='17:00:00'
我想获取日期和时间大于给定日期的行。这是我的 table:
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
谁能帮帮我?
编辑: 为了更清楚,我编辑了我的 post。这是执行查询的 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();
Now the question is: Is the query in the selectEvent correct?
试试这个,
由于 Evdate
和 EvTime
SELECT * FROM table_name WHERE EvDate >'18.01.2015' OR (EvDate = '18.01.2015' AND EvTime > '17:00:00'
更新-
附加一个 fiddle ,会有帮助。
这可能对您有所帮助
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')
SELECT *
FROM table1
WHERE EvDate + EvTime > '2015-01-18T17:00:00'
相反,您可以考虑创建一个添加日期和时间的 PERSISTED 计算列。持久计算列将允许索引
ALTER TABLE table1 ADD cEVdatetime AS EvDate + EvTime PERSISTED
我假设 EvDate 数据类型是 Date 并且 EvTime 数据类型是 Time 。 试试这个....
select * 来自你的表名 其中 CONVERT(VARCHAR(20),EvDate ,112)>=20150118 AND EvTime >='17:00:00'