torquestomp
torquestomp

Reputation: 3334

SQL use column name alias without SELECTING

I know I can specify a column alias like so:

SELECT stuff as mask

Is there a way I can specify a column alias without returning that column data in the result set? Essentially, I want to be able to make my query clean by doing:

SELECT doManipulationStuff(cleanCompactAlias)

Where

reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias

Upvotes: 8

Views: 5339

Answers (2)

Jeff Rosenberg
Jeff Rosenberg

Reputation: 3572

You can use a Common Table Expression (CTE) to create a subquery with aliases:

WITH clean_cte AS
(
  SELECT reallyLong.misleading.andAnnoying.columnName1 as cleanCompactAlias1,
    reallyLong.misleading.andAnnoying.columnName2 as cleanCompactAlias2
)
SELECT doManipulationStuff(cleanCompactAlias1), 
  doManipulationStuff(cleanCompactAlias2)

That way you can put all of your aliasing in the CTE and just forget about it when calling your functions. You still have to do the aliasing somewhere, but this at least keeps it out of your main query to make it a bit more readable.

Upvotes: 4

Gordon Linoff
Gordon Linoff

Reputation: 1269503

You could use a subquery:

select doManipulationStuff(cleanCompactAlias)
from (select t.*, reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias
      . . .
     ) t

Upvotes: 3

Related Questions