0

I'm having an issue which I've been trying to solve for the past hour, and I can't seem to work it out. I've tried Googling for an answer, but haven't found one that works in my case, or at least I haven't been able to get it working.

Here's my code:

@foreach (var row in db.Query("SELECT * FROM PersonnelFiles WHERE (Badge LIKE '#1003' OR Badge LIKE '#1004' OR Badge LIKE '#1005' AND Archived is not true) ORDER BY Badge ASC"))
{
    <li>@row.Rank - @row.Name, @row.Badge</li>
}

So according to my knowledge which is limited, it should now return all entries where badge is like "#1003", "#1004" or "#1005", provided that these are not Archived.

But when trying to do this, it gives me an error: "Incorrect syntax near false".

I hope you can help me, thank you in advance.

EDIT: Archived is a bit datatype.

Mikkel
  • 1,853
  • 1
  • 15
  • 31

2 Answers2

2
SELECT * 
FROM PersonnelFiles 
WHERE (
         Badge LIKE '#1003' OR 
         Badge LIKE '#1004' OR 
         Badge LIKE '#1005' AND 
         Archived AND Archived <> 1
       ) ORDER BY Badge ASC

is the same as

 SELECT * 
    FROM PersonnelFiles 
    WHERE (
             (Badge LIKE '#1003') OR 
             (Badge LIKE '#1004') OR 
             (Badge LIKE '#1005' AND Archived <> 1)
           ) ORDER BY Badge ASC

What i supose you mean is

 SELECT * 
        FROM PersonnelFiles 
        WHERE 
                 (
                    Badge LIKE '#1003' OR 
                    Badge LIKE '#1004' OR 
                    Badge LIKE '#1005'
                 ) AND Archived <> 1
                ORDER BY Badge ASC

se SQL Logic Operator Precedence: And and Or

Community
  • 1
  • 1
Richard L
  • 1,211
  • 7
  • 10
0

try this:

there is nothing like false in SQL SERVER Bit has 1 or 0 values

@foreach (var row in db.Query("SELECT * FROM PersonnelFiles WHERE 
    (Badge LIKE '#1003' OR Badge LIKE '#1004' OR 
    Badge LIKE '#1005' AND Archived <>1) ORDER BY Badge ASC"))
{
    <li>@row.Rank - @row.Name, @row.Badge</li>
}

SQL SERVER DataTypes

Dgan
  • 10,077
  • 1
  • 29
  • 51
  • Thank you for your quick answer, however, it gives me no errors, but rows where Archived is true still show up. – Mikkel Oct 05 '14 at 10:47
  • I will suggest you go to `Sql Server Management Studio` and Execute that SQL Query and You can observe Your Output – Dgan Oct 05 '14 at 10:50