BVernon
BVernon

Reputation: 3747

Compatibility of "column as alias" vs "alias = column"

I've found that over the years my habits have changed and I much prefer writing queries like this:

select FriendlyName = some_column from some_table

as opposed to this:

select some_column as FriendlyName from some_table

Lately I've been reviewing scripts written by some SQL experts which were designed to work across multiple database versions and platforms and I only ever see the latter syntax.

This was never a concern for me before as a software developer knowing specifically what version of SQL Server my code would be run on, but now I'm writing code that I would like to make as accessible to as many versions/platforms as possible. At least, I'd like to write it so the least amount of modifications are necessary to run on additional versions when the time comes.

That said, my question is whether the former syntax (alias = some_column) has always been part of the SQL specification and will run on any database, or would it not work in some platforms or older versions?

Upvotes: 0

Views: 86

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269823

The alias = syntax is specific to SQL Server. The standard is either:

<expression> as alias
<expression> alias

That is, the as is optional -- although I strongly encourage using it.

It is a shame that you have adapted to a syntax that is not available in other databases, and can actually mean other things. In most other databases that accept the syntax, it will be interpreted as a boolean comparison -- and often generate an error because alias is not defined.

And, unfortunately, this syntax convention makes it harder to SQL Server to support a real boolean type, which would be a convenience.

Upvotes: 5

Related Questions