• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to search data in column from value1 to value 2 in SqLite  RSS feed

 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, how do i query data in column from value1 to value 2.
currently am only able to query between two values
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
select * from mytable where myfieldvalue between value1 and value2;
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:select * from mytable where myfieldvalue between value1 and value2;

hi , i already tried that one. I want it inclusive of value 1 and value 2
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SELECT * from TABLE where COLUMN between 1 and 10

will select anything between 1 and 10, inclusive. But instead of just going back and forth with things like that, why don't you post some actual examples of what you need, and explain why an ordinary select statement like that doesn't work for you?
 
james kinyua
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, below is my code snippet. misteriously the code does not work for some values. let say if i want to search for employees who reported to work in a specific day (d1 and d2 are equal).
How do i querry when d1=d2?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james kinyua wrote:hi, below is my code snippet. misteriously the code does not work for some values. let say if i want to search for employees who reported to work in a specific day (d1 and d2 are equal).

Sounds to me like a collision of a date and a timestamp, but exactly how you resolve it I'm not absolutely sure.

The MySql DATE() function returns only the date from an SQL DATE object, but whether you can compare this to a String, even if it's a formatted date, I don't know.

Just one of the reasons I hate SQL.

Winston
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you would be far better off using a PreparedStatement and using java.sql.Date objects for the parameters. What you have, it looks like to me, converts a date object to a String in some format which is unknown to the reader and then asks the database to interpret the String as a date. There's plenty of scope for things to go wrong when you do that. And as matter of policy you should use PreparedStatement wherever possible; in general it prevents SQL injection attacks on your database and specifically it avoids ugly format conversion issues like what may be happening here.

Hopefully those columns are actually declared as DATE type in your database...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!