Когда я изменил жестко закодированную дату (выделено) на ? Возвращено сообщение об ошибке преобразования. Что это за исправление? Цените всю помощь, которую я могу получить.

SET NOCOUNT ON

DECLARE @lastdayofweek DATETIME
DECLARE @firstdayofweek DATETIME
DECLARE @previouslastdayweek DATETIME
DECLARE @previousfirstdayweek DATETIME

SET @lastdayofweek = '2018-08-03'
SET @firstdayofweek = dateadd(day, - 6, @lastdayofweek)
SET @previouslastdayweek = dateadd(day, - 1, @firstdayofweek)
SET @previousfirstdayweek = dateadd(day, - 6, @previouslastdayweek)

SELECT DISTINCT b.unittype
    ,b.propertyid
    ,d.propertyname1
    ,count(CASE 
            WHEN b.ActMadeReadyDate <= @previouslastdayweek
                AND b.occustatus IN ('NA')
                AND b.calcdate = @previouslastdayweek
                THEN b.UnitType
            END) AS 'Last Week Unit Type Not Rent Ready'
    ,count(CASE 
            WHEN b.occustatus IN (
                    'VL'
                    ,'VA'
                    )
                AND b.calcdate = @lastdayofweek
                THEN b.unitid
            END) AS 'Vacants'
    ,count(CASE 
            WHEN b.ActMadeReadyDate <= @lastdayofweek
                AND b.occustatus IN (
                    'VA'
                    ,'VL'
                    )
                AND b.calcdate = @lastdayofweek
                THEN b.unitid
            END) AS 'Rent Ready'
    ,count(CASE 
            WHEN b.ActMadeReadyDate IS NULL
                AND b.occustatus IN ('VA')
                AND b.calcdate = @previousfirstdayweek
                THEN b.unitid
            END) AS 'Rent not Ready'
FROM unitwithstatus b
INNER JOIN property d ON b.propertyid = d.propertyid
LEFT JOIN lease a ON a.propertyid = b.propertyid
    AND a.bldgid = b.bldgid
    AND a.unitid = b.unitid
    AND a.moveindate >= @previousfirstdayweek
    AND a.moveindate <= @previouslastdayweek
WHERE b.propertyid LIKE '01___'
    AND b.unitstatus <> 'W'
    AND b.unitstatus <> 'D'
--and lastdayofweek = ?     --'" & Range("J4") & "'
--and b.PropertyId in ('01TRL')
GROUP BY b.unittype
    ,b.propertyid
    ,d.propertyname1

0