Invalid Column problem

  • This is probably a trivial question, but I am stuck, so I would appreciate it if someone could help me:

    Consder this SELECT statement -

    SELECT a.abc , a.def, DATEDIFF(day,CAST(b.BeginDate),GetDate()) AS mydays FROM Assignment a LEFT OUTER JOIN BusinessObject b ON Assignment.boo_id = BusinessObject.boo_id WHERE mydays > 20

    I am getting an Invalid Column mydays.

    As you can see, mydays is a calculated column.

    ???

    Thanks,  J

     

     

  • Your use of CAST is incomplete. Do you mean

    cast(b.begindate as datetime) perhaps?

    Phil

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • yes, sorry.

    cast(b.begindate as daytime)

  • I am not sure sql * server allows you to use alias to calculated/normal columns in the where clause.

    One alternative is to repeat the calculated column expression in the where clause also

    SELECT a.abc , a.def, DATEDIFF(day,CAST(b.BeginDate),GetDate()) AS mydays FROM Assignment a LEFT OUTER JOIN BusinessObject b ON Assignment.boo_id = BusinessObject.boo_id

    WHERE DATEDIFF(day,CAST(b.BeginDate,GetDate())  > 20

     

     

  • Correct, it doesn't - would be damn useful though.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply