You can use a derived table if you don't want to repeat the column definition.
select case when foo='blah'
then 'fizz'
else 'buzz'
end as bar
FROM
(
select 'blah' as foo
) derived
Or a CTE
;
With blahs As
(
select 'blah' as foo
)
select case when foo='blah'
then 'fizz'
else 'buzz'
end as bar
FROM blahs
A quick test shows the execution plan for all three versions below are the same
SELECT foo,
CASE
WHEN foo='blah'
THEN 'fizz'
ELSE 'buzz'
END AS bar
FROM ( SELECT
CASE
WHEN [number] % 5 = 0
THEN 'blah'
ELSE 'notblah'
END AS foo
FROM [master].[dbo].[spt_values]
)
D ;
WITH blahs AS
( SELECT
CASE
WHEN [number] % 5 = 0
THEN 'blah'
ELSE 'notblah'
END AS foo
FROM [master].[dbo].[spt_values]
)
SELECT foo,
CASE
WHEN foo='blah'
THEN 'fizz'
ELSE 'buzz'
END AS bar
FROM blahs
SELECT
CASE
WHEN [number] % 5 = 0
THEN 'blah'
ELSE 'notblah'
END AS foo,
CASE
WHEN
CASE
WHEN [number] % 5 = 0
THEN 'blah'
ELSE 'notblah'
END='blah'
THEN 'fizz'
ELSE 'buzz'
END AS bar
FROM [master].[dbo].[spt_values]