Answered by:
Set DateTime field to null

Question
-
I'm writing a C# program to create .CSV files from SQL Server tables for a conversion process.
Two questions.
1)I have to get the latest term date out of set of records which may be null if there is no term date. Any suggestions on how to do this?
2)How to I set a DateTime field to null?
Thanks,
MarcieFriday, January 18, 2008 1:36 PM
Answers
-
Does it have to be null? What about some other default value?
Did you know that you can do this?
Code BlockDateTime dealTerm = default(DateTime);Setting objects to null has tendency to cause them to get disposed. You can wind up with null reference errors at compile time and run time.
Rudedog
Friday, January 18, 2008 2:56 PM
All replies
-
Hi,
1) I don't understand the question?
2) Do you mean an SQL server field or a DateTime variable?Friday, January 18, 2008 1:42 PM -
Hopefully this helps:
1)I didn't know how to query using the null, so I set the null date to 12/25/2020. I need to know how to set it back to null in the end result or else I need to know how to query it using null instead of this made up date.
string sqlQuery = "select b.feetype, ' ' as feedescrip, feename, min(deal_eff), ";
sqlQuery += "max(case when deal_term is null then '2020-12-25' else deal_term end),";
2) Here's what I'm trying to do to the DateTime variable
DateTime dealTerm;
dealTerm = (DateTime)objRow2[4];
if (dealTerm.Equals(new DateTime(2020, 12, 25)))
want to set date to null if - 12/25/2020Friday, January 18, 2008 1:50 PM -
Does it have to be null? What about some other default value?
Did you know that you can do this?
Code BlockDateTime dealTerm = default(DateTime);Setting objects to null has tendency to cause them to get disposed. You can wind up with null reference errors at compile time and run time.
Rudedog
Friday, January 18, 2008 2:56 PM -
Hello,
See the link :
http://msdn2.microsoft.com/en-us/library/2cf62fcy(VS.80).aspx
DateTime may be declared as nullable, that's to say no value may be affected to it
Declaration of a nullable DateTime :
DateTime? nulldate;
DateTime notnulldate ;
To test if there is value affected
if ( nulldate.HasValue )
{
/* has a value */
notnulldate = nulldate.Value;
}
else
{
/* no value for nulldate
}
First question
The utilisation of null value is easy for database data : the comparison is made with DBNullrd[
if you have a SqlDataReader rd and a SQLCommand cmd :
DataReader rd = cmd.ExecuteReader();
while ( rd.Read() )
{
if ( rd["deal_term"].Equals(DbNull.Value) )
{
// null date
}
else
{
// not null date
}
}
You can use the class System.Convert which defines DbNull and IsDbNull. See the links :
http://msdn2.microsoft.com/en-us/library/6e3z9zx8(VS.80).aspx
and
http://msdn2.microsoft.com/en-us/library/w495sek6(VS.80).aspx
I hope this post will be useful
Have a nice day
- Proposed as answer by Java. _ Monday, March 22, 2010 12:01 PM
Friday, January 18, 2008 2:57 PM -
UPDATE:
Yup, someon was faster then me
, ql
Cheers,
Jakub G------------------------------------------
Hi
What about DateTime? - nullable date time. This could help you. But please keep in mind that before you will access the value you need to check if it is null or not - otherwise exception will be thrown.
Cheers,
Jakub G
Friday, January 18, 2008 3:02 PM -
Rudedog2 wrote: Does it have to be null? What about some other default value?
Did you know that you can do this?
I was able to go this route and set the DateTime.MaxValue()
Thanks everyone.
MarcieFriday, January 18, 2008 3:10 PM