0
id    date       temp       prcp
1   2015-01-01  -27.18      0
1   2015-01-02  -25.9       1.03
1   2015-01-03  -17.89      9.44
1   2015-01-04  -17.89      9.44
1   2015-01-05  -17.89      9.44

SELECT * FROM tbl WHERE id=1 and date between '2015-01-03' and '2015-01-05'

How do I fix the SQL query above? I am trying to extract all rows between the 2 dates '2015-01-03' and '2015-01-05'

6
  • Did you mean date >= '2015-01-03' and date <= '2015-01-05'? Commented Mar 31, 2017 at 5:22
  • that works! If you put it as answer, I will accept. Also any ideas why between does not work? Commented Mar 31, 2017 at 5:24
  • I don't know why it doesn't work. Does your underlying sql support between? Commented Mar 31, 2017 at 5:28
  • hmm, I think so, I am using postgreSQL Commented Mar 31, 2017 at 5:31
  • 1
    I saw an example of a complex query where they put the BETWEEN expression in parens, to separate BETWEEN ... AND from logical AND: SELECT ... WHERE (x BETWEEN v1 AND v2) AND other stuff. Maybe put your between in parens? Commented Mar 31, 2017 at 5:37

2 Answers 2

2

Did you mean date >= '2015-01-03' and date <= '2015-01-05'?

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

1 Comment

Which is exactly the same as date between '2015-01-03' and '2015-01-05'
2

I think "date" as the column name is a bad idea. Try quoting them with double quote.

1 Comment

postgresql.org/docs/current/static/… "In Table C-1 in the column for PostgreSQL ... If not so marked, a reserved key word is only allowed as an "AS" column label name." and date is not marked.

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.