Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Way to get just date or time from DateTime field in SQL? 3

Status
Not open for further replies.

artyk

Programmer
Apr 5, 2006
163
US
Hi, I am trying to do some comparisons between fields in a database and values in DateTimePicker controls. For some of those I need just the date or just the time. I have received help on getting what I need from the DateTimePicker, but I'm not sure how to get the info I need from the actual database field. Is there a quick and dirty way to pull in just the date or just the time for a comparison? I'm pretty new to .NET and I wasn't sure how to go about this. Thank you in advance.
 
the .Value component of the DateTimePicker control is a Date variable. So, to do what you want, you can Dim a date variable, assign the value from the database to this variable, and then use the same code you used with the DateTimePicker to pull out the part of the date you want - the date part or the time part.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
artyk,
Try and convert your datefield as you pull it from the database.
CONVERT(VARCHAR,'NAME OF DATE FIELD',101)

This Conversion should return just the date part.
Matt
 
Thanks to you both. It may be a while before I know if it worked or not. The section of code that I'm working on now has been pretty intensive, at least for a newbie like me. I appreciate the help, though, and I'm confident your solutions will work. Thanks again!
 
Ok, here's an update. I've got everything set up pretty much like I want it to be. The issue is this: I need to send a date parameter to the db via a query (that has already been set up). The field that I'm using for comparison is the date/time field that I mentioned earlier. If I send just the date I don't get any results because of the missing time component. However, if I send the date and time component I don't get any results either because the times don't match. Is there any way to use a wildcard in a query that would allow me to send something like "04/27/2007*" so that I could get anything that started with that date? I've tried a couple of things, but none have made a difference. I appreciate your help so far. It has allowed me to get pretty close to nailing this procedure down.
 
Assuming you want to extract records for a specific date (DateRequired) from a field (DateField) then one of the easiest way to compensate for time element is to use something like:

WHERE DateField >= DateRequired AND DateField < (DateRequired +1 Day)

Hope this helps.

[vampire][bat]
 
That seemed to work. Thanks, earthandfire.
 
You may also find interesting to run this through query analyzer:

select cast(getdate() as varchar(11))

Furthermore,

select cast(cast(getdate() as varchar(11)) as datetime)

has the effect of truncating time values.

Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top