Why this query is not returning any value. There are values in my database with in that range
value input are
Date startDate = (Date) data.get("startDate");//2014-04-01
Date endDate = (Date) data.get("endDate");//2014-04-30
int pagesize=10;
sql query is
String query = "select * from bill_details "
+ " where status=? "
+ " AND date(add_date) BETWEEN " + startDate + " AND " + endDate
+ " order by add_date desc limit " + pagesize;
ps = con.prepareStatement(query);
ps.setString(1, dataStatus);
System.out.println("Before execution");
res = ps.executeQuery();
Ravinder Reddy
24.1k6 gold badges54 silver badges86 bronze badges
asked Apr 19, 2014 at 7:55
xrcwrn
5,33517 gold badges72 silver badges130 bronze badges
2 Answers 2
Try this:
Date startDate = (Date) data.get("startDate");//2014-04-01
Date endDate = (Date) data.get("endDate");//2014-04-30
int pagesize=10;
String query = "select * from bill_details "
+ " where status=? "
+ " AND date(add_date) BETWEEN ? AND ?"
+ " order by add_date desc limit ?";
ps = con.prepareStatement(query);
ps.setString(1, dataStatus);
ps.setDate(2, startDate);
ps.setDate(3, endDate);
ps.setInt(4, pagesize);
System.out.println("Before execution");
res = ps.executeQuery();
answered Apr 19, 2014 at 8:14
Hamlet Hakobyan
33.5k6 gold badges55 silver badges71 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
jmail
this
right one, try this code, your not set the value from the database,..this problem of your codeYou're trying to concatenate a Date object to a String. Even if that works, then the date isn't surrounded with quotes in the query.
Use SimpleDateFormat to format the Date to a String.
Try this:
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String query = "select * from bill_details "
+ " where status='active' "
+ " AND date(add_date) BETWEEN date('" + df.format(startDate) + "')"
+ " AND date('" + df.format(endDate) + "') " +
+ " order by add_date desc limit " + pagesize;
answered Apr 19, 2014 at 8:07
John Bupit
10.7k10 gold badges46 silver badges79 bronze badges
1 Comment
xrcwrn
So date is treated as string
default
where status=??status='active'BETWEENis rarely the correct operator for dates - What do BETWEEN and the devil have in common?, or Bad habits to kick : mis-handling date / range queries