-1

I get this error:

Incorrect syntax near '2020-05-29'.

How to fix this?

SELECT Borrower.BorrowedDate, BorrowerDetails.DueDate 
FROM Borrower, BorrowerDetails 
WHERE Borrower.BorrowedDate BETWEEN '2020-05-28' AND BorrowerDetails.DueDate '2020-05-29'
Dale K
  • 25,246
  • 15
  • 42
  • 71
SHIRYU
  • 1
  • 2
  • 4
    Use correct syntax for the `WHERE` clause - `WHERE Borrower.BorrowedDate BETWEEN '2020-05-28' AND '2020-05-29'`. Also consider using an unambiguous date format and correct casting `WHERE CONVERT(date, Borrower.BorrowedDate) BETWEEN '20200528' AND '20200529'` – Zhorov May 29 '20 at 07:35
  • What are you trying to achieve with two columns in your between clause? Try to create your statement with greater/lesser ( > / < )instead of between. – TomStroemer May 29 '20 at 07:37
  • I want between table1.From and table2.To – SHIRYU May 29 '20 at 07:38
  • 2
    Be sure to read [this](https://sqlblog.org/2011/10/19/what-do-between-and-the-devil-have-in-common) as well – HoneyBadger May 29 '20 at 07:39
  • You can't use between for that. And you should be using proper joins. – Dale K May 29 '20 at 07:44
  • @Zhorov that format is unambiguous. I assume BorrowedDate is a date type, making your convert superfluous. – TomC May 29 '20 at 07:46
  • Does this answer your question? [SQL query to select dates between two dates](https://stackoverflow.com/questions/5125076/sql-query-to-select-dates-between-two-dates) – Drag and Drop May 29 '20 at 08:02

2 Answers2

0

Between doesn't seem to support two fields.

You may use:

SELECT Borrower.BorrowedDate, BorrowerDetails.DueDate 
FROM Borrower, BorrowerDetails 
WHERE Borrower.BorrowedDate >= '2020-05-28' AND BorrowerDetails.DueDate <='2020-05-29'
Dale K
  • 25,246
  • 15
  • 42
  • 71
Bijeesh K G
  • 181
  • 2
  • 9
  • Doesn't seems is an under statement https://learn.microsoft.com/en-us/sql/t-sql/language-elements/between-transact-sql?view=sql-server-ver15. The sql syntax is clear. – Drag and Drop May 29 '20 at 08:40
-1

BETWEEN doesn't work in that format. You need to change your data format or use epoch time for these dates.

SELECT Borrower.BorrowedDate, BorrowerDetails.DueDate
FROM Borrower, BorrowerDetails
WHERE Borrower.BorrowedDate >= 20200528
AND BorrowerDetails.DueDate <= 20200529;

Don't forget to remove single quotes . We are comparing it like numbers , not like strings.

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • Sure it does work with original format too (it may depend on connection language however). – Arvo May 29 '20 at 08:11