Solved

SQL Server table date field to delete a date and accept a null or blank

Posted on 2011-02-14
4
415 Views
Last Modified: 2012-06-27
I have a VB.Net edit form with a date field that is optional.  Somtimes a date will be added and at other times the user will delete the date and leave it blank.  The sql field is set to accept nulls and is fine if it is left blank.  When I add a date and then delete a date, I get a sql error that the date format is not correct.  how do I get the table to accept the blank as a null again?
0
Comment
Question by:rtay
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 34892234
What is your error? You can add blank value to the date field.
declare @table table(col datetime)
insert @table values (null),(GETDATE()),('')
select * from @table
/*
col
NULL
2011-02-14 14:04:47.063
1900-01-01 00:00:00.000
*/

Open in new window

0
 
LVL 7

Expert Comment

by:lundnak
ID: 34892236
You needs to pass a null value to the date field.  Make sure it isn't an empty set value.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 34892309
SQL Server does implicit data type conversions of strings to dates/datetimes, but does not convert empty strings to NULL.

Check out DBNull.Value. Eg you set Object.Datetimefield = DBNull.Value in VB.NET before saving.

Bye, Olaf.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 34895594
I think the problem is not with the SQL Server but with the control being used. The DateTimePicker does not support Null as its date value.

Try one of these nullable datetimepickers

http://www.codeproject.com/KB/selection/Nullable_DateTimePicker.aspx

http://www.codeproject.com/KB/selection/NullableDateTimePicker.aspx

http://www.codeproject.com/KB/selection/NDTP_VS2008.aspx
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question