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
 General SQL Server Forums
 New to SQL Server Programming
 Select NULL

Author  Topic 

Suhi100
Starting Member

14 Posts

Posted - 2015-02-18 : 06:24:55
Hello,

Can someone explain and possibly give and example on what

SELECT NULL means and how it works?

I often see this expression in views for example,e select NULL as field_name

Thanks

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-02-18 : 09:54:10
SELECT NULL means return no value. SELECT NULL as field_name means return no value and call it field_name
Go to Top of Page

Suhi100
Starting Member

14 Posts

Posted - 2015-02-18 : 12:58:20
Thanks Gerald,

and what is the practical use of it? Is it used to check if there are rows in a table or view?



Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2015-02-18 : 13:10:14
quote:
Originally posted by Suhi100

Thanks Gerald,

and what is the practical use of it? Is it used to check if there are rows in a table or view?




We'd need to see the code to be able to tell you why it is being done.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-02-18 : 13:10:57
Say you want to populate a table with default data, you might write

insert into mytable (a, b, c)
select 1, NUll, 2

if you want to purposefully put no data in column b, perhaps for some particular test cases.

It's not hard to think up other uses as well. I often see it as a placeholder in a UNION:

select a,b,c from abc_data
union all
select d, e, null from de_data

since the union operator requires the same number (and types) of columns for all rowsets
Go to Top of Page

Suhi100
Starting Member

14 Posts

Posted - 2015-02-19 : 04:09:35
It makes sense as I saw it in a UNION operator so as I understand it is because the number of columns have to match.
Thanks for the answers it is clear now
Go to Top of Page
   

- Advertisement -