SQL: What does NULL as ColumnName imply
Asked Answered
Y

8

19

I understand that AS is used to create an alias. Therefore, it makes sense to have one long name aliased as a shorter one. However, I am seeing a SQL query NULL as ColumnName What does this imply?

SELECT *, NULL as aColumn
Yawning answered 28/6, 2013 at 17:10 Comment(3)
can you show the whole query or something more... like select '1' as numbering from tablenamePossie
@sac added query as it standsYawning
I think LittleBobbyTables's answer is right, it is a NULL value for aColumn for all rows...Possie
S
21

Aliasing can be used in a number of ways, not just to shorten a long column name.

In this case, your example means you're returning a column that always contains NULL, and it's alias/column name is aColumn.

Aliasing can also be used when you're using computed values, such as Column1 + Column2 AS Column3.

Semibreve answered 28/6, 2013 at 17:13 Comment(2)
I think so too ;) As to why they wrote the query like that, it's anyone's guess. As it's written, it really doesn't do anything other than return all the columns, plus an extra column called aColumn. Without seeing any surrounding code or business processes, it's hard to guess what the intent of the query is.Semibreve
And the datatype of this column is intDeathful
W
4

When unioning or joining datasets using a 'Null AS [ColumnA] is a quick way to make sure create a complete dataset that can then be updated later and a new column does not need to be created in any of the source tables.

Wolfson answered 12/2, 2019 at 20:47 Comment(0)
A
3

Query result can have a new column that has all NULL values. In SQL Server we can do it like this

SELECT *, CAST(NULL AS <data-type>) AS as aColumn

e.g.

SELECT *, CAST(NULL AS BIGINT) AS as aColumn
Argillite answered 31/5, 2017 at 10:19 Comment(0)
A
2

It means that "aColumn" has only Null values. This column could be updated with actual values later but it's an empty one when selected.

---I'm not sure if you know about SSIS, but this mechanism is useful with SSIS to add variable value to the "empty" column.

Ankus answered 28/6, 2013 at 17:18 Comment(2)
just to be sure, SSIS means: SQL Server Integration Services ?Yawning
Yes. A query as you posted could work as source table, then variable set in SSIS could be used to provide values for the empty column.Ankus
J
2

When using SELECT you can pass a value to the column directly. So something like :

SELECT ID, Name, 'None' AS Hobbies, 0 AS NumberOfPets, NULL AS Picture, '' AS Adress

Is valid.

It can be used to format nicely a query output when using UNION/UNION ALL.

Jacquelynejacquelynn answered 12/9, 2014 at 0:17 Comment(0)
C
1

In the statement result we have a column that has all NULL values. We can refer to that column using alias.

In your case the query selects all records from table, and each result record has additional column containing only NULL values. If we want to refer to this result set and to additional column in other place in the future, we should use alias.

Cuticula answered 28/6, 2013 at 17:13 Comment(0)
N
0

How about without using the the as SELECT ID , Name , 'None' AS Hobbies , 0 AS NumberOfPets , NULL Picture

Nonrecognition answered 4/3, 2015 at 19:33 Comment(0)
U
0

Usually adding NULL as [Column] name at the end of a select all is used when inserting into another table a calculated column based on the table you have just selected.

UPDATE #TempTable SET aColumn = Column1 + Column2 WHERE ...

Then exporting or saving the results to another table.

Upsweep answered 14/9, 2015 at 13:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.