Null Dates are not 1/1/1900

D

David Lozzi

So, I am sending my sql proc a value of '' or null for a date field. When
the record saves, the field has 1/1/1900, not a null. How can I fix this?

Thanks!!

David Lozzi
 
W

William Tasso

David Lozzi delphi-ts.com> said:
So, I am sending my sql proc a value of '' or null for a date field. When
the record saves, the field has 1/1/1900, not a null. How can I fix this?

Check the db schema - does the date column have a default value?
 
S

Smithers

You have to specifically send the value DBNull.Value if you want the
underlying column in your db to store the <NULL> value:

This works (assumes startDate is a string that may hold a valid date or be
zero-length):

object startDateToGo; //defined as object because 'object' can hold the
value - DBNull.Value
if(startDate.Length == 0){
startDateToGo = DBNull.Value;
}
else {
startDateToGo = Convert.ToDateTime(startDate);
}
SqlParameter pStartDate = new SqlParameter("@StartDate",
SqlDbType.DateTime);
pStartDate.Value = startDateToGo;
cmd.Parameters.Add(pStartDate);


-HTH

-S
 
G

gabe garza

select PeriodEndDate, convert(varchar, isnull(PeriodEndDate, ''), 101) as
ConvertDate, cast(isnull(PeriodEndDate, '') as varchar) as CastDate
from TableName

PeriodEndDate ConvertDate CastDate
------------------ -------------------------- ------------------------------
NULL 01/01/1900 Jan 1 1900 12:00AM

Are you using isnull() instead of just SELECTing the date column by itself.
 
K

Kevin Spencer

DateTime values are stored as numbers. A SQL Server DateTime value can
represent a date between January 1, 1900, through June 6, 2079. Whe you put
a DateTime into a column, you put it in as a string. Putting a blank string
in apparently is interpreted as 0, which would represent January 1, 1900. A
blank string is not a null value. Put NULL into the field instead, and make
sure that the column accepts NULL values.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top