'Conversion failed when converting the varchar value 'Lagtime' to data type int

I am trying to use the code below to count the difference in days between the encounter begin date and submission date.

SELECT e.EncounterId,
       e.EncounterBeginDate, 
       rf.SubmissionDate,
       dateDiff(day, e.EncounterBeginDate, rf.SubmissionDate) AS Lagtime, 
       rf.HealthPlanCode,
       rf.TransactionTypeId 
FROM       udt_encounter AS e
INNER JOIN udt_receivedfile AS rf 
        ON e.ReceivedFileId = rf.ReceivedFileId
WHERE rf.healthplancode = '015'
  AND rf.transactiontypeid = 1
  AND rf.submissiondate BETWEEN '2021-07-01' AND '2021-10-31'
  AND Lagtime > 90
ORDER BY Lagtime ASC

I can get the query to run, however when I try to omit any findings under 90 days, I keep getting this error:

Conversion failed when converting the varchar value 'Lagtime' to data type int.
sql


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source