BETWEEN operator with dates
i have to get data between twwo dates but i have two question:
1) if the two dates in the Between operator are not including int the database,Is there aby problem? I mean if i want to get all data in Jan and may i compare between 1/1/07 to 30/1/07?
2) how could exactly i wrote it in java i wrote it like that:
ResultSet rs = stmt.executeQuery("SELECT IN FROM Stat WHERE Event_date BETWEEN '"+1/02/07+"' AND '"+28/02/07+'");
but i have exception
[556 byte] By [
student14a] at [2007-11-26 18:38:52]

# 2
it's not depends on java language, but depend on database system (rdbms) that you used.
for example for oracle database, you can use this syntax
String date1 = "1-Feb-07" ;
String date2 = "28-Feb-07";
ResultSet rs = stmt.executeQuery("SELECT IN FROM Stat WHERE Event_date BETWEEN '"+ date1 +"' AND '"+ date2+ "'");
# 4
> it's not depends on java language, but depend on
> database system (rdbms) that you used.
>
> for example for oracle database, you can use this
> syntax
> > String date1 = "1-Feb-07" ;
> String date2 = "28-Feb-07";
> ResultSet rs = stmt.executeQuery("SELECT IN FROM Stat
> WHERE Event_date BETWEEN '"+ date1 +"' AND '"+ date2+
> "'");
>
for mysql database
String date1 = "2007-02-01" ;
String date2 = "2007-02-28";
ResultSet rs = stmt.executeQuery("SELECT IN FROM Stat WHERE Event_date BETWEEN '"+ date1 +"' AND '"+ date2+ "'");
# 5
i think using PreparedStatement is a much better idea:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = format.parse("2007-02-01");
Date date2 = format.parse("2007-02-28");
String sql = "SELECT inRange FROM Stat WHERE Event_date BETWEEN ? and ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setDate(1, date1);
ps.setDate(2, date2);
ResultSet rs = stmt.executeQuery();
IN is a keyword in SQL. I think it's a bad idea to use keywords as column or table names.
%