0

I have a string

it contains can contains either of (1)

 strQuery = "TempTable.Group_No IN ()";

(2)

strQuery = "TempTable.Group_No IN (1,2,3,4,....)";

My task is if it contains "TempTable.Group_No IN ()", (i.e) IN CLAUSE without data

i have to replace the string (IN() with IN (NULL)) with "TempTable.Group_No IN (NULL)"

How to perform it in C#?

3 Answers 3

2

How about...

strQuery = strQuery.Replace("()", "(NULL)");

... or is that a bit too simple?

Sign up to request clarification or add additional context in comments.

Comments

2

I don't know how much you have to worry about the empty parenthesis in other areas, but you can use this to have greater certainty that you're replacing the right thing.

strQuery = strQuery.Replace("IN ()", "IN (NULL)");

Comments

2

I'm assuming the input is an array of integers for the solution below. Try this out as a console app:

static void Main(string[] args)
{
    int[] ids1 = new int[] { 1, 2, 3, 4, 5 };
    int[] ids2 = new int[] {};

    Console.WriteLine(FormatQuery(ids1));
    Console.WriteLine(FormatQuery(ids2));           
}

static string FormatQuery(int[] ids)
{
    return string.Format("TempTable.Group_No IN ({0})", 
        ids.Length > 0 ? string.Join(",", ids) : "NULL");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.