sbornstein2
asked on
ASP.Net/C# - Dynamic Where Clause Should be Easy
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER