NULL – SQL Server Syntax Example: NULL – T-SQL Example

3-Minute Video Tour of LearnVisualStudio.NET by Bob Tabor


Learn Visual Studio Video Tour



Ideal Tools Organizer


Premium (Not Free) Video Tutorials

Free Video Tutorials & Free Tools

NULL – SQL Server Syntax Example: NULL – T-SQL Example

Purpose: – Illustrates the for the .

SYNTAX:

SELECT [ ALL | DISTINCT ]
[TOP ( expression ) [PERCENT] [ WITH TIES ] ]
column_list
[ INTO new_table ]
[ FROM { table_source } [ ,...n ] ]
[ WHERE expression IS [ NOT ] NULL
[ GROUP BY ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC ]

PURPOSE:
NULL is used in comparisions to determine if column is NULL

 

Code Sample for NULL:

 T-SQL |   |? 
 
/* NULL example from http://idealprogrammer.com 
 
 PURPOSE: NULL is used in comparisions to determine if column is NULL
 
 SYNTAX: 
 
 expression IS [ NOT ] NULL
 
 
 
 
 SELECT [ ALL | DISTINCT ] 
			[TOP ( expression ) [PERCENT] [ WITH TIES ] ] 
			 column_list, ISNULL(check_expression, replacement_value)  
			[ INTO new_table ] 
			[ FROM { table_source } [ ,...n ] ] 
			[ WHERE column IS [ NOT ] NULL] 
			[ GROUP BY ] 
			[ HAVING  search_condition  ] 
			[ ORDER BY  order_expression [ ASC | DESC ] ] 
*/
 
 
CREATE TABLE people(
     ID          INT,
     firstname   VARCHAR (20),
     lastname    VARCHAR (20),
     statecode   VARCHAR (2),
     alive       BIT,
     height      INT )
 GO
 
 
 
 
 INSERT INTO people (ID, firstname, lastname, statecode, alive) --height is null 
 VALUES (1,  'Paul', 'Revere', 'AL', 0)
 GO
 
 INSERT INTO people (ID, firstname, lastname, statecode, alive, height) 
 VALUES (2,  'Pat', 'Lennon', 'NY', 0, 69)
 GO
 
 INSERT INTO people (ID, firstname, lastname, statecode, alive, height)
 VALUES (3,  'Peter', 'Martin', 'NY', 1, 75)
 GO
 
 INSERT INTO people (ID, firstname, lastname, statecode, alive, height)
 VALUES (4,  'George', 'Washington', 'VA', 0, 75)
 GO
 
 
 
 -- Select entire table
 SELECT 'Entire Table', * FROM people
 
 -- 1. Example of using IS NOT NULL
 
 SELECT 'Example #1' AS Example, firstname, lastname, height 
  FROM people p
  WHERE height IS NOT NULL
 
 GO
 
 -- 2. Example of using IS NULL
 
 SELECT 'Example #2' AS Example, firstname, lastname, height 
  FROM people p
  WHERE height IS NULL
 
 GO
 
 -- 3. Example showing that you should not use = with NULL
 --    Expecting 1 row but getting none 
 --    Comparison operators return UNKNOWN when either or both arguments are NULL.
 
 SELECT 'Example #3' AS Example, firstname, lastname, height 
  FROM people p
  WHERE height = NULL
 
 GO
 
 -- 4. Example of how COUNT ignores NULLS
 --    4 rows in table, but COUNT(height) returns a count of 3
 
 SELECT 'Example #4' AS Example, COUNT(height)
  FROM people p
 
 
 GO
 
 
 
 
 
 DROP TABLE people;
 
 GO
 
 
 
 
 
 

Sample Output for NULL Example

SQL Select NULL

Related posts:

  1. IS NULL – SQL Server Syntax Example: IS NULL – T-SQL Example IS NULL - SQL Server Syntax Example: IS NULL -...
  2. DISTINCT – SQL Server Syntax Example: DISTINCT – T-SQL Example DISTINCT - SQL Server Syntax Example: DISTINCT - T-SQL Example...
  3. ISNULL – SQL Server Syntax Example: ISNULL – T-SQL Example ISNULL - SQL Server Syntax Example: ISNULL - T-SQL Example...
  4. HAVING – SQL Server Syntax Example: HAVING – T-SQL Example HAVING - SQL Server Syntax Example: HAVING - T-SQL Example...
  5. NOT – SQL Server Syntax Example: NOT – T-SQL Example NOT - SQL Server Syntax Example: NOT - T-SQL Example...

Related posts brought to you by Yet Another Related Posts Plugin.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

Powered by WP Hashcash

This blog uses the cross-linker plugin developed by Web-Developers.Net