1

I am trying to create a select statement that checks with two values but checks one with null, i created this so far,

string selectStatement 
    = string.Format(@"SELECT a, b, c 
                      FROM Table1 
                      WHERE c IS NOT NULL AND a = {0} AND b = {1}",
                    aValue, bValue);

But I am pretty sure it will not work as we can't have 2 ands in select statement, any idea;s please ?

Michael Bray
  • 14,998
  • 7
  • 42
  • 68
Mathematics
  • 7,314
  • 25
  • 77
  • 152

2 Answers2

2

You can have as many ANDs in your WHERE clause as you like.

If a and b are string values then you need quotes around the values - though it'd be much better to use a parameterized query and avoid the Sql Injection vulnerability

SELECT a, b, c 
FROM Table1 
WHERE c IS NOT NULL 
AND a = @p0
AND b = @p1
Mark Brackett
  • 84,552
  • 17
  • 108
  • 152
juergen d
  • 201,996
  • 37
  • 293
  • 362
1

There is no reason for that not to work, what DB are you using?

If in doubt you can also add parenthasis:

(c IS NOT NULL) AND ((a = value) AND (b = value))

s1cart3r
  • 334
  • 1
  • 4
  • 9