[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

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!
0
BlakeMcKenna
Asked:
BlakeMcKenna
  • 6
  • 2
  • 2
  • +2
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nemws1Commented:
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
 
HuaMinChenBusiness AnalystCommented:
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
 
nemws1Commented:
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
 
BlakeMcKennaAuthor Commented:
I figured it out myself...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now