Question

I'd like to reuse an alias that contains a non-alphabetic character in a query, something like:

 SELECT 42 AS "the#answer", "the#answer"+8 AS "fifty";

Output I want: 42|50; output I get: 42|8.

I've tried almost every possible combination of quote types, and looked for documentation, but I can't seem to find a working solution.

Any idea?

Was it helpful?

Solution

SQL cannot refer to an alias from the same output clause in which it was introduced. (It has nothing to do with quoting, which only allows otherwise invalid identifiers; some SQL vendors would have thrown an error, but SQLite appears "more relaxed" in the handling of this case.)

You could use a nested query (sqlfiddle).

SELECT fortytwo, fortytwo + 8 as fifty
FROM (
    SELECT 42 AS fortytwo)

This works because the referenced identifier, fortytwo, was introduced in a "previous" output clause.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top