How to get precise dates from using SQL query
I have made a simple program in VB.Net.
In this program I made two DataTimePicker with the names of
1. DtpFrom
2. DtpTo
I want to get the dates from tblStudents between the dates given in the dtpFrom and dtpTo.
The Date Format for these two DateTimePicker and in tblStudents is "M/d/yyyy".
When I run this query.
Code:
"Select AdDate from tblStudents where AdDate >= '" & Format(dtpFrom.value,"M/d/yyyy") & "' and AdDate <= '" & dtpTo & "'"
it gives me some times correct results but some times it missed some of the dates.
I want to get the precised dates, without missing any date. So is there any Mechanism to solve this Problem.
Re: How to get precise dates from using SQL query
The value from a DTP does not have a format - it is just a date value (which is stored internally in a way you would not even recognise, such as the number 35839.4553).
When it comes to SQL statements, you need either to convert all values to a string formatted in the correct manner (as you seem to have done with dtpFrom, but we can't be certain without knowing which database system you are using), or do things the proper way and use Parameters - in which case you do not need to worry about formatting or delimiting, and various other issues.
For a fuller explanation of why you should be using parameters (and links to code examples), see the article Why should I use Parameters instead of putting values into my SQL string? from our Database Development FAQs/Tutorials (at the top of the Database Development forum).
If there are still problems after that, make sure that the AdDate field has a date based data type.
Re: How to get precise dates from using SQL query
I have understood.
I have read the article that Why I should Use Parameters.
But
If you plz Explain that what do you mean by a parameter and how to use it, I will be thankful to you.
Plz give examples if possible.
Thanks
Re: How to get precise dates from using SQL query
The article itself contains a brief generic example, and near the bottom there are links to language examples - as this is VB.Net, you want the "Retrieving and Saving Data in Databases" link.