Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now