PC Review


Reply
Thread Tools Rate Thread

How to convert/insert NULL date value into MS SQL?

 
 
darrel
Guest
Posts: n/a
 
      11th Aug 2005
I have the following right now to enter a date into SQL getting the data
from some pull down menus:

-------------------------------------------------
dim dateCCJApprovedDate as DateTime

if cbx_ccjDateNone.Checked = True then
dateCCJApprovedDate = ctype("", DateTime)
else
dateCCJApprovedDate = ctype(ddl_CCJDateMonth.SelectedValue.tostring &
"/01/" & ddl_CCJDateYear.SelectedValue.tostring,
System.Data.SqlTypes.SqlDateTime)
End If
-------------------------------------------------

That works if there is a date to enter. But fails if there isn't, as "" is a
string and can't be converted to a date/time.

So, I did a bit of googling, and came up with this:

-------------------------------------------------
dim dateCCJApprovedDate as System.Data.SqlTypes.SqlDateTime

if cbx_ccjDateNone.Checked = True then
dateCCJApprovedDate = System.Data.SqlTypes.SqlDateTime.null
else
dateCCJApprovedDate = ctype(ddl_CCJDateMonth.SelectedValue.tostring &
"/01/" & ddl_CCJDateYear.SelectedValue.tostring,
System.Data.SqlTypes.SqlDateTime)
End If
-------------------------------------------------

But I have the opposite problem...I can use the null value, but I can't
convert the second set of data to SQLDateTime.

So, I seem to be trying to use/cast two different types of data to the same
field format in SQL and hence my problem. I'm guessing the second method is
a better approach, but it appears I need to do some sort of intermediate
cast/conversion. Am I on the right track with that line of thinking?

-Darrel



 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      11th Aug 2005

command.Parameters.Add("@date", SqlDbType.DateTime)
if cbx_ccjDateNone.Checked = True then
command.Parameters("@date").Value = DbNull.Value
else
command.Parameters("@date").Value =
cdate(ddl_CCJDateMonth.SelectedValue.tostring & "/01/" &
ddl_CCJDateYear.SelectedValue.tostring)
end if


hopefully nullable types in 2.0 will make this cleaner..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"darrel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have the following right now to enter a date into SQL getting the data
> from some pull down menus:
>
> -------------------------------------------------
> dim dateCCJApprovedDate as DateTime
>
> if cbx_ccjDateNone.Checked = True then
> dateCCJApprovedDate = ctype("", DateTime)
> else
> dateCCJApprovedDate = ctype(ddl_CCJDateMonth.SelectedValue.tostring &
> "/01/" & ddl_CCJDateYear.SelectedValue.tostring,
> System.Data.SqlTypes.SqlDateTime)
> End If
> -------------------------------------------------
>
> That works if there is a date to enter. But fails if there isn't, as "" is
> a
> string and can't be converted to a date/time.
>
> So, I did a bit of googling, and came up with this:
>
> -------------------------------------------------
> dim dateCCJApprovedDate as System.Data.SqlTypes.SqlDateTime
>
> if cbx_ccjDateNone.Checked = True then
> dateCCJApprovedDate = System.Data.SqlTypes.SqlDateTime.null
> else
> dateCCJApprovedDate = ctype(ddl_CCJDateMonth.SelectedValue.tostring &
> "/01/" & ddl_CCJDateYear.SelectedValue.tostring,
> System.Data.SqlTypes.SqlDateTime)
> End If
> -------------------------------------------------
>
> But I have the opposite problem...I can use the null value, but I can't
> convert the second set of data to SQLDateTime.
>
> So, I seem to be trying to use/cast two different types of data to the
> same
> field format in SQL and hence my problem. I'm guessing the second method
> is
> a better approach, but it appears I need to do some sort of intermediate
> cast/conversion. Am I on the right track with that line of thinking?
>
> -Darrel
>
>
>



 
Reply With Quote
 
darrel
Guest
Posts: n/a
 
      11th Aug 2005
> command.Parameters.Add("@date", SqlDbType.DateTime)
> if cbx_ccjDateNone.Checked = True then
> command.Parameters("@date").Value = DbNull.Value
> else
> command.Parameters("@date").Value =
> cdate(ddl_CCJDateMonth.SelectedValue.tostring & "/01/" &
> ddl_CCJDateYear.SelectedValue.tostring)
> end if


So the solution is to use stored procedures?

-Darrel


 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      15th Aug 2005
Parameters can be used with inline sql as well...there's really no excuse
not to use them (and plenty of reasons to do it)



dim c as new SqlCommand("SELECT * FROM Blah WHERE x = @Date")
c.Parameters.Add("@Date", SqlDbType.DateTime).Value = SomeValue

works perfectly.

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"darrel" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>> command.Parameters.Add("@date", SqlDbType.DateTime)
>> if cbx_ccjDateNone.Checked = True then
>> command.Parameters("@date").Value = DbNull.Value
>> else
>> command.Parameters("@date").Value =
>> cdate(ddl_CCJDateMonth.SelectedValue.tostring & "/01/" &
>> ddl_CCJDateYear.SelectedValue.tostring)
>> end if

>
> So the solution is to use stored procedures?
>
> -Darrel
>
>



 
Reply With Quote
 
John.Net
Guest
Posts: n/a
 
      16th Aug 2005
You could also use a regular expression validator and not even allow
the call to the sp to happen if the date is not a valid date.

 
Reply With Quote
 
darrel
Guest
Posts: n/a
 
      16th Aug 2005
> dim c as new SqlCommand("SELECT * FROM Blah WHERE x = @Date")
> c.Parameters.Add("@Date", SqlDbType.DateTime).Value = SomeValue


Thanks, Carl. I definitely need to start playing with paramaters.

-Darrel


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Insert null value in column-type is date/time & format isShortDat billypit786@gmail.com Microsoft Access Forms 0 23rd Apr 2008 03:49 PM
Insert null value in column-type is date/time & format is ShortDate,in Access 2002 billypit786@gmail.com Microsoft Access Forms 2 22nd Apr 2008 09:53 PM
insert null date/time =?Utf-8?B?YXNkZg==?= Microsoft Access 11 27th Apr 2007 02:16 AM
Insert Null Date =?Utf-8?B?YW5u?= Microsoft ADO .NET 3 29th Jun 2006 04:33 PM
Insert null date from XML data to SQL Server Roman Microsoft VB .NET 0 25th Oct 2004 02:39 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:44 PM.