Roger
Roger

Reputation: 97

Incorrect Sytax near ',' in SQL command string (C#)

I have the following command line written in c#, but I keep getting an exception telling me that there is incorrect syntax near ','. Any ideas?

SELECT Name, Age 
FROM testTable 
WHERE Name = 'Roger', Age BETWEEN '19' AND '21', Sex LIKE M%;

Any help is greatly appreciated. Thank you.

Upvotes: 0

Views: 78

Answers (2)

Trevor Pilley
Trevor Pilley

Reputation: 16413

In SQL you must use AND or OR in the where clause:

In addition, you have 2 other syntax errors:

  1. numerics can be literal - use BETWEEN 19 AND 21 if the columns are numeric
  2. The LIKE value needs quoting as it is a string LIKE 'M%'

The result should be this:

SELECT Name, Age FROM testTable WHERE Name = 'Roger' AND Age BETWEEN 19 AND 21 AND Sex LIKE 'M%';

Upvotes: 1

Denys Séguret
Denys Séguret

Reputation: 382274

You must use AND, not a comma, to separate conditions in WHERE :

SELECT Name, Age FROM testTable
WHERE Name = 'Roger' AND Age BETWEEN 19 AND 21 AND Sex LIKE 'M%';

Upvotes: 8

Related Questions