469,292 Members | 1,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

DateTime short formatted range lookup

Hello,

I'm trying to figure out a method to look up by a range of dates,
entries in a database. The format of the date in the database is "M\D
\yyyy HH:MM:SS".

What i need to do is take the dates that are in the database
(formatted as above), isolate the date (which is in short format) and
compare it to the short formatted date in a datetimepicker.

How can I take a string and format it as a short date?

How can I compare two short dates, to find if the date falls in the
date range? (isolate each part of the date? is there an actual
function that handles this?)
Any help is greatly appreciated,

Matt

Aug 7 '07 #1
3 2029
Matt Brown - identify wrote:
Hello,

I'm trying to figure out a method to look up by a range of dates,
entries in a database. The format of the date in the database is "M\D
\yyyy HH:MM:SS".
Why are you storing the date as a string in the database? Store it as a
date.
What i need to do is take the dates that are in the database
(formatted as above), isolate the date (which is in short format) and
compare it to the short formatted date in a datetimepicker.

How can I take a string and format it as a short date?
You can't. You first have to convert it to a date, then you can format
it to a string.
How can I compare two short dates, to find if the date falls in the
date range?
You can't. The date format that you are using is not comparable.
(isolate each part of the date? is there an actual
function that handles this?)
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.

--
Göran Andersson
_____
http://www.guffa.com
Aug 8 '07 #2
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 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

If not, there must be a conversion?
>
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?

I'm guessing that the variable that I'm setting the date value out of
the datebase to, needs to be a datetime?

I'm new at this.
Thanks!

Matt
Aug 8 '07 #3
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
Aug 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

99 posts views Thread by Glen Herrmannsfeldt | last post: by
5 posts views Thread by _Andy_ | last post: by
15 posts views Thread by Fritz Switzer | last post: by
6 posts views Thread by Brandon | last post: by
5 posts views Thread by rbtmaxwell | last post: by
7 posts views Thread by TheLostLeaf | last post: by
1 post views Thread by Magnus.Moraberg | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.