Solved

SQL "SELECT" Statement in VB w/DBNull

Posted on 2013-10-27
7
1,089 Views
Last Modified: 2013-10-28
I have a SQL Select statement with parameters and if the parameter is "null" in the database I have to write out "VariableName IS NULL" instead of

VariableName=@VariableName and then use ...
command.Parameters.Add(@VariableName, SqlDbType.VarChar).Value = DBNull.Value

I don't want to change all of my statement formats to accommodate the nulls - how can I get the Select statement to properly handle a null?

Thanks
0
Comment
Question by:ProdigyOne2k
7 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39604808
You can wrap each nullable column in ISNULL(<columnname>, '')

That's assuming it's a char/varchar/nvarchar type
0
 
LVL 5

Expert Comment

by:MohitPandit
ID: 39604852
Hello,

In case actor pass NULL value in Stored Procedure/Query then should it return 'All' records OR 'NULL' records only. Actually, based on that I can provide query.

Looking forward for your response.

Best Regards,
Mohit Pandit
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39604874
I am having a difficult time understanding you.  Please post your query and maybe that will help.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 39605161
You can change your filter to

where (@VariableName is Null OR VariableName=@VariableName)
0
 

Author Comment

by:ProdigyOne2k
ID: 39605738
ewangoya:

I thought of that route already but I'm trying to avoid adding the "OR" statement to every query that might have a null

BriCrowe:
Here is the query

SELECT ID, ReceivedDate, Qty FROM PartNH WHERE SKU=@SKU AND CUST=@CUST AND ReceivedDate=@ReceivedDate AND LI=@LI ORDER BY LI ASC

Note that "ReceivedDate" is the variable/parameter that sometimes will be NULL
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 total points
ID: 39605746
SELECT ID, ReceivedDate, Qty
FROM PartNH
WHERE ISNULL(@SKU, SKU) = SKU
   AND ISNULL(@CUST, CUST) = CUST
   AND ISNULL(@ReceivedDate, REceivedDate) = ReceivedDate
   AND ISNULL(@LI, LI) = LI
ORDER BY LI
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 39605987
One solution is not to permit nulls in the field in table design. Set the field to a default value that is an impossible date for your application, such as 01-01-5000. If, as is too often seen, your date is recorded in a text field, set the default value of the field to ''.

This way, you do not have to care about nulls in your queries.

In your code in the places where you would normally check for a null, check for 01-01-5000 instead. And you might have to do it a little more often than you do with nulls, because you cannot rely on the database to prevent empty values when the data is updated later on.

The main problem with this approach is that while you save time writing your SQL commands or stored procedures, you must work more everywhere the field is displayed so as to show nothing when the date is 01-01-5000. And if the data is modified by the user, make sure that you save 01-01-5000 if the user leaves the field empty.

No matter what you do, empty fields will always require some work on your part. Its up to you and to how you work with a particular database to decide if you deal with that through nulls or through your code.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

733 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