?
Solved

SQL "SELECT" Statement in VB w/DBNull

Posted on 2013-10-27
7
Medium Priority
?
1,140 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
Independent Software Vendors: 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 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

Independent Software Vendors: 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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Suggested Courses

762 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