操作数类型冲突:日期与int / NOT IN子查询不兼容

时间:2018-04-26 13:02:56

标签: tsql

SELECT Br.BusinessDivision,B.BookingDate, B.DepartureDate, B.CustomerID, C.FirstName, C.SurName
FROM Booking B
    INNER JOIN Customer C
        ON C.ContactID = B.ContactID
    INNER JOIN Branch Br
        ON Br.BranchID = B.BranchID
WHERE B.BookingDate
      BETWEEN '2015-04-01' AND '2018-04-26'
      AND Br.BusinessDivision IN ( 'Retail', 'TVSales' )
      AND B.ContactID NOT IN (
                                 SELECT B.DepartureDate FROM Booking B WHERE B.DepartureDate >= '2018-04-26'
                             );

以下SQL语句给出了以下错误:error Operand type clash: date is incompatible with int

日期列B.BookingDateB.DepartureDate都是数据类型date而不是int所以我不确定为什么会收到此错误?

1 个答案:

答案 0 :(得分:1)

AND B.ContactID NOT IN ( SELECT B.DepartureDate

您正在将ContactID(可能是一个int)与DepartureDate进行比较,大概是一个约会。

我想你想要:

AND B.ContactID NOT IN
(
    SELECT B.CustomerID FROM Booking B
    WHERE B.DepartureDate >= '2018-04-26'
);