Click here to Skip to main content
15,881,599 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
"select * from voucher where trndate between '" & Format(Me.dtp_from.Value, "MM/dd/yyyy") & "' AND '" & Format(Me.dtp_to.Value, "MM/dd/yyyy") & "'"



I am using between to find data according between these two dates which slelct by the user.but i m not gaining any result against this query.
plz help me.

dtp_from is my first date time picker
dtp_to is the 2nd one.

user will select date from dtp_from and dtp_to
Posted
Updated 29-Sep-11 2:43am
v2
Comments
RaisKazi 29-Sep-11 8:46am    
Which RDBMS you are using? MSSQL/MySQL/Orecle?

Try this:
"select * from voucher where trndate between #" & Format(Me.dtp_from.Value, "MM/dd/yyyy") & "# AND #" & Format(Me.dtp_to.Value, "MM/dd/yyyy") & "#"


i have replaced ' with #
 
Share this answer
 
First thing to note is that BETWEEN is Inclusive of the first date, and Exclusive of the second. So if your user specifies
SQL
BETWEEN 2011-02-01 AND 2011-02-02
Only those entries for February 1st will be returned.

In addition, do not convert dates to any format: if you have them as DateTime objects, then pass that through as is using parametrized queries. In this case, you are not at risk from SQL injection attacks, but it is well worth always avoiding concatenated strings to prevent accidental or deliberate destruction of your database.
SQL
SELECT * FROM voucher WHERE trndate BETWEEN @DT1 AND @DT2

VB
SelectCommand.Parameters.AddWithValue("@DT1", Me.dtp_from.Value)
SelectCommand.Parameters.AddWithValue("@DT2", Me.dtp_to.Value)
(It also makes the code easier to read)
 
Share this answer
 
Comments
Mehdi Gholam 29-Sep-11 9:05am    
My 5!
RaisKazi 29-Sep-11 9:08am    
My 5!. Parametrized Queries are always better than plain text queries.
prince_rumeel 29-Sep-11 9:10am    
but it is also not giving any result.
OriginalGriff 29-Sep-11 9:20am    
So, first thing to do: comment out the BETWEEN part, and make sure some data comes back - you should get every record in the database. If that works, then replace the BETWEEN with
WHERE trndate >= @DT1
If that returns records, make a list of them, and try
WHERE trndate < @DT2
If that returns records, check that some of them are also in the previous list.
Let me know what you find out.
prince_rumeel 29-Sep-11 9:30am    
empty record at both time.
basicaly i m designing cristel reportand want to get data in there detail field but no value is there
Try using "yyyy-MM-dd" as a format string for your dates.

Also execute your query string on your database manager application to see if you have any rows.
 
Share this answer
 
Comments
OriginalGriff 29-Sep-11 8:56am    
The OP wrote:
"yyyy-MM-dd i have try this but same there is no result.

in database there are 5 rows

and date is shown in this format 2011-09-29 00:00:00"
prince_rumeel 29-Sep-11 9:05am    
"select * from voucher where trndate between #" & Format(Me.dtp_from.Value, "MM/dd/yyyy") & "# AND #" & Format(Me.dtp_to.Value, "MM/dd/yyyy") & "#"


when i use this then there is an error
incorrect syntax near '#'
You'll probably have to use:
SQL
"select * from voucher where trndate between to_date('" & Me.dtp_from.Value.ToString & "','MM/dd/yyyy') AND to_date('" & Me.dtp_to.Value.ToString & "','MM/dd/yyyy')"
 
Share this answer
 
Comments
prince_rumeel 29-Sep-11 9:38am    
to_date is not a builtin function
Scubapro 29-Sep-11 9:43am    
'To_Date' is a SQL function.
prince_rumeel 29-Sep-11 10:28am    
but i m using oledb
Scubapro 29-Sep-11 10:40am    
Okay. So if your using MySQL, use the equivalent 'STR_TO_DATE'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900