Using keyword NULL next to column in select statement

  • Okay, here's the deal. I have the below select statement I'm trying to analyze but I've never seen this particular use of NULL in a select statement of a query. Here's the query in question.

    SELECT a.colA, a.colB, a.colC, NULL colD, NULL colE,

    NULL colF

    FROM table1 a

    WHERE a.colA = @paramA

    AND (a.colC= 0

    OR @paramA= 1

    OR a.colA = @paramB)

    This is a snippet of the real stored procedure.

    I know about the function ISNULL and using IS NULL and IS NOT NULL but I've never seen NULL used like this in t-sql.

    Any assistance will be appreciated.

  • chinn_chris (9/24/2012)


    Okay, here's the deal. I have the below select statement I'm trying to analyze but I've never seen this particular use of NULL in a select statement of a query. Here's the query in question.

    SELECT a.colA, a.colB, a.colC, NULL colD, NULL colE,

    NULL colF

    FROM table1 a

    WHERE a.colA = @paramA

    AND (a.colC= 0

    OR @paramA= 1

    OR a.colA = @paramB)

    This is a snippet of the real stored procedure.

    I know about the function ISNULL and using IS NULL and IS NOT NULL but I've never seen NULL used like this in t-sql.

    Any assistance will be appreciated.

    This is using the constant NULL as the column and giving it an alias without using the AS.

    This is the same as below:

    SELECT a.colA, a.colB, a.colC, NULL AS colD, NULL AS colE,

    NULL AS colF

    FROM table1 a

    WHERE a.colA = @paramA

    AND (a.colC= 0

    OR @paramA= 1

    OR a.colA = @paramB)

    Hope that helps.

    _______________________________________________________________

    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/

  • Yes, that does help. Thank you for your almost immediate response! I had a feeling it might be that but couldn't find anything online about that particular use.

  • chinn_chris (9/24/2012)


    Yes, that does help. Thank you for your almost immediate response! I had a feeling it might be that but couldn't find anything online about that particular use.

    If you are unfamiliar with that it would be nearly impossible to google I think. It is not limited to using NULL. It is just a column alias with the AS keyword left off. This is NOT a good practice. It is better to be in the habit of explicitly using AS. It has two benefits. First it is not as likely to confuse somebody else who may not have seen this before (like you until today) and secondly, if you forget to add a comma it will change the actual columns in your result, or cause an error that can be a real PITA to find.

    _______________________________________________________________

    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/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply