I am not so into Microsoft SQL Server and I have the following problem.
I have this query:
SELECT
[ID]
,[Numero protocollo]
,[Anno]
,[IdUor]
,[Protocollista]
,[Protocollato]
,[avorato]
,[Errore]
,[CopiaConoscenza]
,[Inoltro]
,[DataProtocollo]
,GETDATE () AS [Today]
,ABS(DATEDIFF(DAY, GETDATE (), [DataProtocollo])) AS [NumberOfDays]
FROM
[PROT_INOLTRO]
WHERE
[PROT_INOLTRO].IdUor = 1
AND [PROT_INOLTRO].Protocollista = 'i:0#.w|iwgroupnet\anobili'
AND [NumberOfDays] < 15
ORDER BY
ID DESC
As you can see, I added 2 calculated columns: [Today]
and [NumberOfDays]
.
As you can see I am filtering using 3 WHERE conditions.
My problem is that I need to filter using the [NumberOfDays]
column that is a calculated column, and not a column defined directly in the PROT_INOLTRO
table.
Executing this query I get the following error message:
Msg 207, Level 16, State 1, Line 23
Invalid column name 'NumberOfDays'
Why can't I use this calculated column in my WHERE
condition? How can fix my query to obtain this behavior?
Use like this:
...
AND ABS(DATEDIFF(DAY, GETDATE (), [DataProtocollo]))<15