Solved

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

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

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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, …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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