Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
i have this queryselectdatediff(dd,getdate(),getdate()+3) as Diff, -- getdate()+3 is an examplecase when (Diff < 7) then 1when (Diff >=7 and Diff < 14) then 2when (Diff >=14 and Diff < 21) then 3when (Diff >=21 and Diff < 28) then 4end as WeekNumberi get this error Invalid column name 'Diff'.how do i calculate diff then use it within the same query without doing declare @diff int ...etc?thanks
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2006-06-08 : 00:10:46
you can't use the column alias in the select statement except in the ORDER BY.I changed the select datediff .. as Diff as a derived table
select Diff, case when (Diff < 7) then 1 when (Diff >=7 and Diff < 14) then 2 when (Diff >=14 and Diff < 21) then 3 when (Diff >=21 and Diff < 28) then 4 end as WeekNumberfrom( select datediff(dd,getdate(),getdate()+3) as Diff -- getdate()+3 is an example) a