logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 How to pass NULL date parameters to a stored procedure using ADO Sybase.

Posted on
2/18/2001
Author:
Robert Gelb
Email:
Not Shown
Applies To OS:
NT, 9x, 2000
Product:
5, 6



Update.  We intend to create a resource center for all things Sybase.  It is a pretty good database, however, for people who have gotten used to MS type of customer support, Sybase support and resources may not seem as thorough.  Soon, you'll see here various stuff regarding making Sybase databases work with ADO/OLEDB.  For now, read this article.

OLEDB provider and ODBC driver for Sybase seem to have this nasty bug, where it is impossible to pass NULL to a Date parameter in a stored procedure. For instance, consider the following piece of code:


cmd.Parameters.Append cmd.CreateParameter("@mat_date", adDBTimeStamp, adParamInput, 0)
'This causes various errors when .Execute method is run
cmd.Parameters("@mat_date").Value = NULL	
'This statement just basically passes 1, which the database translates to Jan 1, 1899 (???)            
cmd.Parameters("@mat_date").Value = vbNull
The error messages vary from provider to provider (and 
one even locks up the program), but the gist of it is that the provider is 
expecting a data type with a certain Precision and 
NumericScale.  The error is somewhat misleading, since it is just convering up a bug.

So what should you do to fix this problem?  Nothing.  Yep, absolutely nothing.  Not populating the date parameter will actually pass NULL to your stored proc.  In reality, what happens is by not setting the date parameter, the value stored in the parameter will be Empty (yes, there is such a value in VB).  That, for some strange reason, will force VB/ADO/ODBC to pass NULL to the stored procedure.


Private Sub Populate_SP_DateParameter(dt as Date, prm as ADODB.Parameter)
    If Not IsNull(dt) then
        prm.Value = dt
    End If
End Sub
Or alternatively you can simply set the stored proc parameter to Empty.

cmd.Parameters("@mat_date").Value = Empty


Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word:
 



Comments
#1. By JasonZ. Posted on 5/5/2006 4:39:53 PM
Great tip! This really helped me solve my problem.

#2. By jignesh. Posted on 5/9/2006 7:36:57 AM
please give me code for from date to todate

#3. By Hideki. Posted on 12/11/2006 7:33:28 PM
That looks nice but here when I put Empty returns this message:
Default parameters values are not suppported by this database
How can I solve this?
Null and vbNull didn't work too...

#4. By Anonymous. Posted on 9/4/2008 10:21:05 AM
in .NET 2.0 of greater the right way to do it is
cmd.Parameters("@mat_date").Value = DBNULL.value

#5. By siyam. Posted on 9/21/2009 6:26:31 PM
you da man!!!!!!!!!!!!!!!!!!!!

#6. By Marcelo. Posted on 4/22/2010 3:07:34 PM
Hi Hideki, the alternative is the best way but not sending Empty, the correct code is

cmd.Parameters["@mat_date"].Value = DBNull.Value;