Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


ASP.Net/C# - Dynamic Where Clause Should be Easy

Posted on 2006-04-20
Medium Priority
Last Modified: 2012-06-27
Hello all.  I have a dynamic SQL string I am creating and the Where Clause has an IN in the statement.  What I need to do is some of the parts in this IN statement can be blank "".  I need to omit those in the IN statement and only have the IN statement run with parts that have a value.  For example,

Where part_num IN ('" + partNum1 + "','" + partNum2 + "','" + partNum3 + "','" + partNum4 + "','" + partNum5 + "')

partNum4 could be blank so I need to leave that out.  Any idea on how to easily build this with something clean.  Thanks all.
Question by:sbornstein2
  • 2

Accepted Solution

GENTP earned 2000 total points
ID: 16501841
How about puting the parts in an array and looping through it, testing each one for blank?

Author Comment

ID: 16501954
Yep that is what I can do I actually already have an array for it.  I am just trying to find out the cleanest least code to now check for each one being blank and building just the inner part of the IN statement ().

Expert Comment

ID: 16502486
I'd just basically loop through, if the partnum isn't null, concatenate it into a string with a comma on the end of it. so using the string sqlIn:

sqlIn+=partNum.ToString() + ",";

Then after the loop I would check if the entire sqlIn was null or not. If it's not, strip off the last character (that trailing comma, you never know how many parts will be in there) and then stick the sqlIn string right into the brackets of your original sql string.

It sounds like you're probably already doing this, or something very similar though.

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Integration Management Part 2
Suggested Courses

571 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