SQL uses a column name alias without SELECTING

I know that I can specify a column alias as follows:

SELECT stuff as mask

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

SELECT doManipulationStuff(cleanCompactAlias)

Where

reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias
+5
source share
2 answers

You can use the Common Table (CTE) expression 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)

This way you can put all your aliases in the CTE and just forget about it when calling your functions. You still need to do aliases somewhere, but this at least doesn't allow you to execute the main request to make it more readable.

+4
source

You can use a subquery:

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

All Articles