Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

c# .net DateTime without Time

Is there a way of setting a DateTime, and persisting that to SQL Server DateTime field and distinguish between those with times and those without, without using a 'special' time like midnight?
E.g. I want a datetime field to hold NextAppointmentDate, which may or may not have a Time, but I don't want to store a seperate Time field, is that possible?
0
Silas2
Asked:
Silas2
1 Solution
 
s_chilkuryCommented:
I think this question is in regards of SQL DateTime field.

If you wish to store a date (NextAppointmentDate) then you need to store that in Date field in case of SQL Server 2008. This is not the case with SQL Server 2005 or less. In that case you need to store date field excluding time specifying the time as 00:00:00.
0
 
Silas2Author Commented:
You can't say 'the time element is null' somehow? Its just that someone might want to use midnight.
0
 
s_chilkuryCommented:
Yes, for SQL Server 2005 or below versions we dont have time element as NULL. But with SQL Server 2008 we have a new datatype which is date and time as separate datatypes.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Jini JoseSenior .Net DeveloperCommented:
you can use the datetime field.
and while passing the datetime value from c# you can format it with yyyy-Mm-dd hh:mm:ss.

if it has time then the datetime value will be like this 2011-Mar-03 10:20:33
and if it not has time then the datetime value will be like this 2011-Mar-03 00:00:00

0
 
Silas2Author Commented:
Yes gmailjini: but that is midnight, isn't it? Say someone wanted to use midnight? wouldn't that get confused with 'time=null'?
0
 
Todd GerbertIT ConsultantCommented:
The answer is that you need two separate fields in your database to accomplish this, one for Date and one for Time. .Net has no notion of a date-only type.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now