Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Standard syntax for specifying new column name?

Author  Topic 

GunnerMcGrath
Starting Member

1 Post

Posted - 2008-10-23 : 13:38:03
Hi all, just a simple question about the generally accepted way to write a bit of SQL.

When specifying a new column name for calculated columns, I know there are many syntaxes that will work, all with the same result. It seems that the most common is as follows:

SELECT col1
, col2
, col1 + col2 AS SumColumns
FROM table

Now personally, I don't like doing it this way because when you have very large computations, subqueries, case statements, etc., it puts your column name at the end of the statement which makes it hard to know what you're looking at until you search for the column name, especially if you're selecting dozens of columns.

My personal preference was to write it like this:

SELECT col1
, col2
, "SumColumns" = col1 + col2
FROM table

This puts the column name at the front where it's easy to see, and the double quotes aren't used anywhere else in my SQL so I always know double quotes means a column name.

At my new job, they've demanded I not use double quotes due to their SQL standards here. They said I can use single quotes, or no quotes at all, which both work. Unfortunately both of these styles are misleading because it could be read to mean that those are either strings or already-existing column names.

Just curious if anyone else has some input on a nice way to write my queries now that I've been forbidden from using double quotes. It's all in subjective but I'd like to hear a few opinions and find out if there's a generally accepted way to do this.

On the other hand, if you can give me a good reason why their "no-double-quotes" rule for SQL is stupid so I can argue them out of it, that would be just as good =)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-23 : 13:41:12
I believe the ANSI way is via AS.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -