Storing a NULL value in a DateTime column in SQL Server?

I have a VB.Net Windows App that updates a SQL Server 2008 DB. In this particular table are several columns defined as "datetime". Is it possible to store a NULL value in a "datetime" column? If so, how?

Thanks!
BlakeMcKennaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sirbountyCommented:
Try using NOTHING (not NULL) in your code, or post the code snippet for more focused help.
0
QPRCommented:
Dim someDate As SqlDateTime
someDate = SqlDateTime.Null
0
QPRCommented:
I'm assuming you want to update a value in the column with NULL rather than do an insert?
If an insert then simply don't include the date column in your INSERT statement and you will have your NULL value.
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.

nemws1Database AdministratorCommented:
You should check to see if your table allows a NULL value in that column:
USE databasesname;
GO
EXEC sp_help 'tablename'

Open in new window

Check the 'nullable' column in the second of results to whichever column correlates to the DATETIME field you're trying to set to NULL.  It could be that it does *not* allow NULL values.
0
HuaMin ChenProblem resolverCommented:
Do like this
update tab1
set datecol=null
...

Open in new window

0
BlakeMcKennaAuthor Commented:
I tried using this way

Dim someDate As SqlDateTime
someDate = SqlDateTime.Null

Open in new window


However, in VB.Net SqlDateTime.NULL does not exist...
0
BlakeMcKennaAuthor Commented:
Here is the code in question:

cmd.Parameters.AddWithValue("@calibrationDate", clsHE.calibrationDate = If(clsHE.calibrationDate.ToString = "#12:00:00 AM#", Nothing, clsHE.calibrationDate))
cmd.Parameters.AddWithValue("@nextCalibrationDate", clsHE.nextCalibrationDate = If(clsHE.nextCalibrationDate.ToString = "#12:00:00 AM#", Nothing, clsHE.nextCalibrationDate))

Open in new window

0
BlakeMcKennaAuthor Commented:
Here is a screenshot...
Screenshot.jpg
0
sirbountyCommented:
Does this work for you?

Dim nullValue as system.dbnull
'[...]
cmd.Parameters.AddWithValue("@calibrationDate", clsHE.calibrationDate = If(clsHE.calibrationDate.ToString = "#12:00:00 AM#", nullValue, clsHE.calibrationDate))
cmd.Parameters.AddWithValue("@nextCalibrationDate", clsHE.nextCalibrationDate = If(clsHE.nextCalibrationDate.ToString = "#12:00:00 AM#", nullValue,clsHE.nextCalibrationDate))

Open in new window

0
BlakeMcKennaAuthor Commented:
sirbounty...I tried that and it still didn't work. In my stored procedure, I have the variables setup like this:

	@extensionDate             DATETIME=NULL,
	@calibrationDate           DATETIME=NULL,
	@nextCalibrationDate   DATETIME=NULL,

Open in new window


Can the way these are defined be different?
0
nemws1Database AdministratorCommented:
You've defined default values for your stored procedure parameters.  That's good.  However, if you want that default value to be in effect, you would simply *not* provide them when you call your stored procedure (assuming you're using named parameters, which you are).

If you want these values to be NULL, then do *not* add these parameters when you call the stored procedure.  I would think some IF() statements around the two cmd.Parameters.AddWithValue() lines would solve your issue.
0
BlakeMcKennaAuthor Commented:
I figured it out. Below is the change that I made!

New Code:
cmd.Parameters.AddWithValue("@calibrationDate", SqlDbType.DateTime).Value = If(clsHE.calibrationDate.ToString = "1/1/0001 12:00:00 AM", DBNull.Value, clsHE.calibrationDate)
cmd.Parameters.AddWithValue("@nextCalibrationDate", SqlDbType.DateTime).Value = If(clsHE.nextCalibrationDate.ToString = "1/1/0001 12:00:00 AM", DBNull.Value, clsHE.nextCalibrationDate)
cmd.Parameters.AddWithValue("@extensionDate", SqlDbType.DateTime).Value = If(clsHE.extensionDate.ToString = "1/1/0001 12:00:00 AM", DBNull.Value, clsHE.extensionDate)

Open in new window


Old Code:
cmd.Parameters.AddWithValue("@calibrationDate", clsHE.calibrationDate = If(clsHE.calibrationDate.ToString = "1/1/0001 12:00:00 AM", DBNull.Value, clsHE.calibrationDate)
cmd.Parameters.AddWithValue("@nextCalibrationDate", clsHE.nextCalibrationDate = If(clsHE.nextCalibrationDate.ToString = "1/1/0001 12:00:00 AM", DBNull.Value, clsHE.nextCalibrationDate)
cmd.Parameters.AddWithValue("@extensionDate", clsHE.extensionDate = If(clsHE.extensionDate.ToString = "1/1/0001 12:00:00 AM", DBNull.Value, clsHE.extensionDate)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
I figured it out myself...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

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.