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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# and SQL: Date Fields

Status
Not open for further replies.

Pluto87

Programmer
Oct 7, 2011
23
US
Hello,

I have a program connected to SQL. There are two date fields, Birth_Date and a Death_Date.
We have a program that enters information with these two fields and other numerous fields.

When a user enters a Birth_Date, the value shows (for example) is "03/20/2012 12:00:00 AM".
The Date is correct, but the time is off. I'm having trouble getting the system to show the exact time this data was entered.

Also, for Death_Date, this field can accept NULLS, but when a user leaves this field blank, a default is entered - "1/1/1900 12:00:00 AM".
I'm also having trouble getting the system to accept NULLS. But when there is a date for the Death_Date field, it needs to accept the date and the current time.

strSQL += " , Birth_Date = '" + BirthDate.Text.ToString() + "'";
strSQL += " , Death_Date = '" + DeathDate.Text.ToString() + "'";

Any help and suggestions would be help! Thanks!
 

When a user enters a Birth Date are they just entering a date, if so then SQL will hoe this in a datetime filed so the default time would be set to 12:00am, you would need to deal with this in the formatting on your frontend programme.

When you have a null value in a datetime field the default display is the result you are seeing, should be no problem to work around when you are pulling the data out.

You talk about entering the date of death but adding the current time to it, what happends if the date of death was yesterday, or 7 hours ago. Surely you would want to enter either the date of death or the date and time of death.



I love deadlines. I like the whooshing sound they make as they fly by
Douglas Adams
(1952-2001)
 
Thank you for your reply. I should have mention that birth_date and death_date isn't really the field names we are dealing with it. I am using birth_dates and death_dates as an example.

We need to keep track of the of the date and current time of the data that was enter from the user.

The user will enter a Birth_date, but not the current time that was date was enter. In the database, we need to see the date the user entered and the time it was entered.

If there is not a death_date, the database (SQL Server) needs to accept a NULL as the data. And since SQL defaults the death_date to "1/1/1900 12:00:00 AM", I need to figure out how to program it in my codes. This is what I need help on.

Thanks, suggestions and help is appreciated!
 
Since you are only entering the date, you need to get the time from somewhere else. One thing you can do is use GETDATE(), strip out the date from that, and concatenate the two values (inputted date and the time from GETDATE()) into one value.

As for returning a NULL value, you could code it so that when the date is 1/1/1900 it returns NULL or blank or whatever instead.

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Thank you for your responses and help! I will try both of the methods you two provided!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top