Tuesday, March 27, 2012

Field attribute question

If I want to store day of the week (i.e. Mon), do I use datetime field or char? Also if I update #25/12/2004# to a datetime field. What is the value is the time port of the field? Thx.I would calculate the day of the week from a datetime, if you were also saving the datetime. If you are saving ONLY a day of week, then I would use a number, etc.

With only a date stored in a DateTime, the time would be 00:00:00.000|||I created two fields in a sql table, one for storing date and one for time. In my VB, I populated them with #3/8/2005# and #10:00:00 PM# respectively. The program crashed with message "sqldattime overflow. Must be between 1/1/1753 12:00:00 AM ...." Why did the program crash? Should I combine two fields into one?|||Show some code.|||In my aspx form I created one text field and one drop down list. The value in the text field is pulled from MS calendar control so it is like 3/10/2005 and the ddl is filled with time in every 15 min. e.g. 10:00, 10:15.
In my vb code, I coded as following:


...
reservation.requesteddate=cdate(txtbkdate.text)
reservation.requestedtime=ddlRequestedTime.SelectedItem.Text
...
reservation.insert()

I also did some experiment in which I created a temp table and a smalldatetime field and I entered only 10:00. When I debugged, I found it actually stored a date and time and the date value is one day (can't remember exactly) before the valid range.

Should I combine two fields into only one? Thanks.|||DateTime fields ALWAYS contains a date and a time. You can use code to split them for independent display, if you wish. I certainly would combine them in a single field if they refer to the same event.|||Thanks, Doug. Yes, they do refer to the same event. What I intended to do is a field for booked date and another for booked time. I though by splitting into two fields, when I create tree view for booking for a particular date will be comparatively easier. But still, apart from such reason, if I do have a need to only store a time value in a datatime field, what is the proper way?|||The reality is, you ALWAYS store both, and then use client side (in this case, ASP.NET) formatting to show the data you want. Alternately, you can use SQL code to get just the date or time (see CONVERT() in SQL Server Books Online).|||Doug, here is another real situation I have, can I have your comments.
I have a table to store my restaurant's info and I need to store the business hours. I created two fields to store from and to. Before I create a form to maintain the info of this table, I simply input data from vs2003 and it did accept my entry (only 10:00 entered to a smalldatetime field). Now if I create a form for data entry, apparently it makes no sense to ask user to input date and time, so I have to do something in the vb code. But which is a more sensible way to do so? Should I just concatenate any date with the time entered by user and update it to sql table? How do other developers normally do? Since I'm developer from other platform, may be my though is totally unlogically...|||I would likely concatentate the date and time, then parse it into a single DateTime (I generally do not use SmallDateTime's). The question is, is the date and time a single attribute? Meaning, are you talking about 3/13/2005 10:15 AM as a single thing, so that at some point, you might want to find a row based upon that date/time combination.|||Sorry I may have confused you. Put it in a simply way, what I need to store is just only a time, the date is meaningless to me. So really I don't need to store a date to the database but according to what you said, the date portion is required when popuplating the database. It means that I have to put in a meaningless date even I don't need it. The way I see is if documentation is not done properly or the field is not named meaningfully then other programmers may be misled by the date when examining data. I hope this is clear for you to understand my point.

No comments:

Post a Comment