Home > OS >  SQL Server: How to write conditional WHERE clause containing different queries per condition?
SQL Server: How to write conditional WHERE clause containing different queries per condition?

Time:12-15

I found a bunch of answers on StackOverflow in regards to conditional WHERE clauses, but I wasn't able to find one that was somewhat specific to my issue, so just looking for a little help...

My company's website contains all SQL queries written directly within the C# code (I'm not a fan of that, but that's another story) & I'm having an issue trying to update a query that contains multiple conditions in the WHERE clause based on which checkboxes a user checks off on the form.

Provider Types

Within the form is a set of checkboxes (currently 3 options, but this list will potentially grow). For each "Provider Type" selected, an additional condition needs to be added to the WHERE clause but I'm unsure how to best handle this.

Here's the code that handles this. ProviderTypesCSV can contain up to 3 values (0,1,2) based on how many options the user checked:

if (!String.IsNullOrEmpty(searchOptions.ProviderTypesCSV))
{
    // values are in a csv string.  Split out the csv
    var statuses = searchOptions.ProviderTypesCSV.Split(',');

    if (statuses.Contains("0"))   //Hospitals & Facilities
    {
        strSQL  = " AND (<<perform query for 'Hospitals & Facilities'>>)";
    }

    if (statuses.Contains("1"))  //Physicians and Practitioners
    {
        strSQL  = " AND (<<perform query for 'Physcians and Practitioners'>>)";
    }

    if (statuses.Contains("2"))   //In-Person Visit Providers
    {
        strSQL  = " AND (<<perform query for 'In-Person Visit Providers'>>)";
    }
}

The current issue obviously, is the above query doesn't take into account when multiple options are selected. When multiple options are selected, the conditional operator needs to be "OR", not "AND", but I'm not sure how to best set this up without making a mess & taking into account add'l options may be added later.

Just curious on the best way to set this up.

CodePudding user response:

You can define a List of string and add your conditions, then concat each element with an " OR " delimiter and use it for your AND

if (!String.IsNullOrEmpty(searchOptions.ProviderTypesCSV))
    {
        //values are in a csv string.  Split out the csv
        var statuses = searchOptions.ProviderTypesCSV.Split(',');
        
        List<string> conditions = new List<string>();

        if (statuses.Contains("0"))   //Hospitals & Facilities
        {
            conditions.Add("(<<perform query for 'Hospitals & Facilities'>>)");
        }
        if (statuses.Contains("1"))  //Physicians and Practitioners
        {
            sconditions.Add("(<<perform query for 'Physcians and Practitioners'>>)");
        }
        if (statuses.Contains("2"))   //In-Person Visit Providers
        {
            conditions.Add("(<<perform query for 'In-Person Visit Providers'>>)");
        }
        
        strSQL  = " AND ("   String.Join(" OR ", list)   ") ";
        
    }
  • Related