Solved

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

Posted on 2011-02-14
4
414 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net Open video relating to control 2 29
Code enhancement 4 32
Disable TLS1.0 on Win 2012 server 7 57
How to get a Powershell script to launch from Visual Studio 20 64
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 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