Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL "SELECT" Statement in VB w/DBNull

Posted on 2013-10-27
7
Medium Priority
?
1,176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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 1000 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

Industry Leaders: 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!

Question has a verified solution.

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

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…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

597 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