Solved

SQL "SELECT" Statement in VB w/DBNull

Posted on 2013-10-27
7
1,113 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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

705 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