Home > SQL Server > Handling Null Values in SQL Server

Handling Null Values in SQL Server

From time to time, we may want to replace NULL values with some desired value when doing an sql select statement. Turns out that SQL Server makes this a simple task – and no IF statements are necessary! To do this, we use SQL Server's ISNULL function. This function takes two parameters. The first parameter is the name of the field that we expect may have a NULL value that we wish to replace. The second parameter represents the replacement value for the NULL value.

So the syntax for the ISNULL function is:

 

ISNULL ( possible_null_value_field, replacement_value )

 

To test the ISNULL function, lets create a table and add some data to it.

 

CREATE TABLE tblEmployees (fname as varchar(50),
         lname as varchar(50),
         gender as varchar(20))

INSERT INTO tblEmployees (fname, lname, gender)

SELECT 'John', 'Brown', 'Male'
UNION ALL
SELECT 'Sara', 'Smith', 'Female'
UNION ALL
SELECT 'Harry', 'Thomas', NULL
UNION ALL
SELECT 'Jennifer', 'Smith', 'Female'
UNION ALL
SELECT 'Jada', 'Reynolds', NULL

 

After running the above query we should have a table that looks like this:

 

tblEmployees Table
fname lname gender
John Brown Male
Sara Smith Female
Harry Thomas NULL
Jennifer Smith Female
Jada Reynolds NULL

 

As you can see we have a couple records with NULL values for the gender field. So if I wanted to replace these NULL values in my SQL Select statement with say…."Not Specified," we would use the ISNULL function in the following manner:

 

SELECT fname, lname, ISNULL(gender, 'Not Specified')
FROM tblEmployees

 

It will produce the following results:

 

Select Statement
fname lname gender
John Brown Male
Sara Smith Female
Harry Thomas Not Specified
Jennifer Smith Female
Jada Reynolds Not Specified

 

I hope this will prove useful to someone out there. Please comment if you have any queries. I will try responding as soon as i can.

Categories: SQL Server Tags: , ,
  1. May 29th, 2010 at 02:54 | #1

    hi wats your myspace page

  2. May 31st, 2010 at 21:26 | #2

    Great article very important information i found here r1hth5HB8Khtmz

  3. June 3rd, 2010 at 11:47 | #3

    @what does my name mean
    Sorry, I don’t have a myspace page.

  4. June 9th, 2010 at 14:41 | #4

    Hi, I’m very interested in Linux but Im a Super Newbie and I’m having trouble deciding on the right distribution for me (Havent you heard this a million times?) anyway here is my problem, I need a distribution that can switch between reading and writing in English and Japanese (Japanese Language Support) with out restarting the operating system.

  5. June 12th, 2010 at 00:50 | #5

    I am sorry, but I am not too familiar with Linux distributions. I wish I could recommend the right distribution for you. A quick google search brought me to Wikipedia’s List of Linux Distributions. I found one in the list called Berry Linux which seems to be a prospect based on your requirements. I hope that helps.

  1. No trackbacks yet.
  • Twitter
  • Buzz
  • Facebook