I am trying to query an sql database and I am having trouble. Is there an error with the syntax below or is it something else that I am missing?
SELECT Name, DateTime, num1, num2
FROM names_E6
WHERE DateTime between '2012-1-27' AND '2012-1-27'
WHERE num1 between '0' AND '5'
WHERE num2 between '0' AND '6'
In addition to the answers regarding the syntax, I would like to add a comment on the usage of value literals:
If num1 and num2 are numbers, the condition should be
Note the missing single quotes around the numbers. Some DBMS will not even allow a conversion from a character literl
'5'to a number, others might surprise you with strange results and most of them will not use an index on the column if such an implicit type casting is successful.You should also be aware that the expression
DateTime between '2012-1-27' AND '2012-1-27'might not always work correctly due to the implicit type casting (String -> Date) and locale settings of the server and/or client. To be on the safe side you should use e.g. the ANSI standard for a date literal:Note the keyword
DATEto start a date literal and the month given with two digits.