September 3, 2015 at 4:04 pm
Was an easy nice question. Thanks.
Thanks.
September 6, 2015 at 5:50 am
Well it's still possible to write the alias in 12 different ways in SQL2014 (deprecated or not) π
SELECT object_id AS test
FROM sys.columns AS mt;
SELECT object_id AS 'test'
FROM sys.columns AS mt;
SELECT object_id AS [test]
FROM sys.columns AS mt;
SELECT object_id test
FROM sys.columns AS mt;
SELECT object_id [test]
FROM sys.columns AS mt;
SELECT object_id 'test'
FROM sys.columns AS mt;
SELECT test = object_id
FROM sys.columns AS mt;
SELECT 'test' = object_id
FROM sys.columns AS mt;
SELECT [test] = object_id
FROM sys.columns AS mt;
SET QUOTED_IDENTIFIER ON;
SELECT OBJECT_ID "test"
FROM sys.columns AS mt;
SELECT OBJECT_ID AS "test"
FROM sys.columns AS mt;
SELECT "test" = OBJECT_ID
FROM sys.columns AS mt;
/HΓ₯kan Winther
MCITP:Database Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
September 8, 2015 at 2:44 pm
Quite a few ways.
September 9, 2015 at 10:51 am
I agree. 3 or 6 is the correct answer. Very bad example for explanation. π
September 10, 2015 at 9:39 am
The answer should be 3, don't like the question; however, I'm not going to complain too much since you take the time to put these questions together.
September 10, 2015 at 10:11 am
Interesting, simple yet tricky question.
Thanks Steve.
September 11, 2015 at 7:51 am
I agree with a lot of people here, the correct answer should be 3, based upon structure, not decorations:
SELECT x AS y
SELECT x y
SELECT y = x
Brackets, quotes, or double quotes around names are just variations on these three (and they make the list uglier, in my opinion; when Management Studio generates a query using brackets around every single column name, I usually select the entire mess and do search and replace to strip them out).
I used to usually use SELECT x AS y, because the AS makes the syntax more explicit. However, after seeing Aaron's blog post a while back, I've switched to using SELECT y = x for new code, because it makes it easier to scan a list of column names. I've seen "stream of consciousness" (SELECT x, y, z, a, b, c) queries too many times, and they drive me nuts.
I think this is a good question, not because it showed a "perfect" way to do something (or NOT to do something), but because it sparked an excellent discussion.
The best thing is that the SQL standard allows all of these variants based upon personal preference, and doesn't force only one way upon everyone.
September 11, 2015 at 8:04 am
stephen.long.1 (9/11/2015)
I agree with a lot of people here, the correct answer should be 3, based upon structure, not decorations:SELECT x AS y
SELECT x y
SELECT y = x
I agree with the number, but not the list π
1 and 2 in your list are the same, just with an optional [AS]
The true third alias form is the positional alias you can use after a subquery, in a CTE, or in a view
Positional:
select foo.bar from ( select 1 ) as foo( bar );
create view foo( bar ) as select 1;
with foo( bar ) as ( select 1 ) select foo.bar from foo;
All three:
with foo[highlight="#ffff11"](bar)[/highlight] as (select [highlight="#ffff11"]hutch =[/highlight] starsky from (select 1 [highlight="#ffff11"]as starsky[/highlight]) as tv ) select foo.bar from foo;
π
MM
select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);
September 11, 2015 at 8:25 am
"How many different ways can you specify an alias for a column in a SELECT clause?"
In this organisation? One. What's known as "Kristen's Way" π
An annoyance I have is that AS is optional. I would really REALLY like an option to disallow various optional features. I find
SELECT Col1 Col2, Col3
a very annoying bug to find, particularly when the thing downstream is using a template to form some user-output and silently removes "{col1}" as "not provided"
September 15, 2015 at 9:16 am
I think there are only 3 ways to asign an alias to a column,
column as alias, alias=column and column alias.
The other variants are diferent ways to write the alias name, not to asign the alias to a column
September 15, 2015 at 9:56 am
freyes (9/15/2015)
I think there are only 3 ways to asign an alias to a column,column as alias, alias=column and column alias.
The other variants are diferent ways to write the alias name, not to asign the alias to a column
Actually you really only posted 2 ways. The first and last are the same thing because AS is optional. π
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 β Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
September 16, 2015 at 1:14 am
Ok, then we must agree that only two ways exists, The other ways are only variations of the two main ways.
September 16, 2015 at 7:28 am
freyes (9/16/2015)
Ok, then we must agree that only two ways exists, The other ways are only variations of the two main ways.
Would this count as a third?
SELECT * FROM (SELECT 1, 2, 3 ) X (A, B, C)
September 16, 2015 at 8:24 am
Really is the most unusual way i have seen, but works!!!
Thanks for your answer
September 16, 2015 at 8:32 am
sestell1 (9/16/2015)
freyes (9/16/2015)
Ok, then we must agree that only two ways exists, The other ways are only variations of the two main ways.Would this count as a third?
SELECT * FROM (SELECT 1, 2, 3 ) X (A, B, C)
That's one I hadn't seen before.
Viewing 15 posts - 46 through 59 (of 59 total)
You must be logged in to reply to this topic. Login to reply