Reputation: 27
Code not recognizing column alias
I am testing codes using PowerShell (sqlcmd) and the code returns an error that it does not recognize column 'Sdate' in Line 3? I have defined 'Sdate' in Column 1 so I am confused, may someone please help as I am new to SQL.
SELECT DATEADD(dd,0, DATEDIFF(dd,0, MyDate)) AS SDate
FROM Schedule
WHERE Sdate > '20180101';
Upvotes: 1
Views: 612
Reputation: 3257
WHERE
is evaluated before SELECT
. This is the reason Sdate
is not recognize. SQL order of operation
FROM
WHERE
GROUP BY
HAVING
WINDOW functions
SELECT
DISTINCT
UNION
ORDER BY
LIMIT and OFFSET
Upvotes: 1
Reputation: 2479
That's SQL not letting you using aliases that way. Repeat the 'function' instead
Do instead
SELECT DATEADD(dd,0, DATEDIFF(dd,0, MyDate)) AS SDate
FROM Schedule
WHERE DATEADD(dd,0, DATEDIFF(dd,0, MyDate)) > '20180101';
Upvotes: 2
Reputation: 32003
alias does not support all dbms in where
SELECT DATEADD(dd,0, DATEDIFF(dd,0, MyDate)) AS SDate
FROM Schedule
WHERE MyDate> '20180101'
Upvotes: 0