Matt Brown - identify wrote:
On Aug 7, 9:43 pm, Göran Andersson <gu...@guffa.comwrote:
>Why are you storing the date as a string in the database? Store it as a
date.
It is stored as datetime.
You said that the date had a specific format, that's why I assumed that
it was a string. A datetime value doesn't have a format.
>You can't. You first have to convert it to a date, then you can format
it to a string.
I don't need to convert it.
>You can't. The date format that you are using is not comparable.
Are they both short dates?
1) M/D/yyyy
2) M/D/yyyy HH:MM:SS
These are date format, not dates. DateTime values doesn't have a format,
they only get a format when you convert them to strings. A DateTime
value always contains a date and a time component.
If not, there must be a conversion?
If you convert the dates into strings in that format, you can't compare
them.
>The DateTime structure has a Date property that gives you the date part,
i.e. a DateTime value where the time part is 00:00:00.
However, you don't need any of that for what you are doing. Get the date
from the DateTimePicker, and calculate the starting and ending time of
the interval. Then you can easily select the records from the database
where the date is between them.
That is of course if you change the database so that you store the date
as a date, so that it's possible to compare it to anything at all.
Can you give me some syntax or examples? for instance, if the date
time pickers are called dateStart and dateEnd, how would i compare the
two?
You do like this in the SQL query:
.... where DateFieldInDataBase >= @Start and DateFieldInDateBase < @End
@Start and @End are parameters. You add Parameter objects to the Command
object with the values from dateStart and dateEnd.
If dateEnd is the date of the last day, you use dateEnd.AddDays(1) to
get the end time.
I'm guessing that the variable that I'm setting the date value out of
the datebase to, needs to be a datetime?
Yes.
--
Göran Andersson
_____
http://www.guffa.com