tags:

views:

133

answers:

2

What is wrong with this statement??

SELECT ID, datediff("mi", Start, End) as Total 
FROM TimeTable
WHERE Total is not null

I get an error "Invalid column name"

+11  A: 

Reference the expression, not the alias.

SELECT ID, datediff("mi", Start, [End]) as Total 
FROM TimeTable
WHERE datediff("mi", Start, [End])  is not null

EDIT, updated to prevent syntax error for usage of END

cmsjr
and wrap End in [ ]
Sk93
lol, that's exactly what my parser just said.
cmsjr
The select clause is processed, logically, prior to the where clause. The aliases are not available until after the select clause is processed. (Almost?) all clauses, except ORDER BY are processed prior to the select clause. Order by can use aliases because it is processed after the select clause.
Shannon Severance
A: 

Don't use reserved words like "End" as table or column names! Use something like TaskStart/TaskEnd or JobStart/JobEnd or StartDate/EndDate, you'll thanks me everytime you don't have to go back and add [] around your table/column names...

KM
it was a name just for an example case..
agnieszka