Solved

SQL "SELECT" Statement in VB w/DBNull

Posted on 2013-10-27
7
1,043 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
Comment Utility
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
Comment Utility
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
Comment Utility
I am having a difficult time understanding you.  Please post your query and maybe that will help.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
You can change your filter to

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

Author Comment

by:ProdigyOne2k
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now