• subscribe
November 20, 2007 12:00 AM

Sharpen Your Basic SQL Server Skills

Brush up on FILLFACTOR, ISNULL, NULLIF, and % as wildcard and operator
SQL Server Pro
InstantDoc ID #97337
Downloads
97337.zip

Executive Summary:

New DBAs get answers to basic Structured Query Language (SQL) questions. This month’s column explores the difference between FILLFACTOR = 0 and FILLFACTOR = 100; ISNULL and NULLIF; a clustered index and a nonclustered index; % used as a wildcard and % used as an operator; and TRUNCATE and DELETE.


This month I’ve been hearing from new DBAs who’ve asked about the difference between FILLFACTOR = 0 and FILLFACTOR = 100 when creating or rebuilding an index and the difference between ISNULL and NULLIF (and how they each compare with CASE). You also want to know how % used as a wildcard differs from % used as an operator. I’ll answer these top questions on the basics and give you some sample scripts. If you can’t wait a month to catch up on SQL Server tips and information, check out my blog at www.sqlmag.com/go/SQLskills.

Q: What’s the difference between using FILLFACTOR = 0 and FILLFACTOR = 100 when creating or rebuilding an index?

A: FILLFACTOR = 0 and FILLFACTOR = 100 are the same in all respects. FILLFACTOR, a percentage measurement, determines how much data is filled on the leaf level of each index page when an index is created or rebuilt. The minimum value for FILLFACTOR is 0, and the maximum value is 100. The default is FILLFACTOR = 0. Once modified from 0 to any other value, the FILLFACTOR value can’t be set again. Setting FILLFACTOR to 100 has the same effect as setting it to the default of 0, because both FILLFACTOR = 0 and FILLFACTOR = 100 tell SQL Server to completely fill the leaf page. No space is unused. Using a value between 1 and 99 always leaves a percentage of the leaf page space unused. For example, FILLFACTOR = 80 leaves 20 percent of the space unused.

Q: What’s the difference between NULLIF and ISNULL?

A: The difference between NULLIF and ISNULL is that NULLIF returns NULL, whereas ISNULL replaces NULL. NULLIF checks for certain parameters and returns NULL if the parameters are equal; otherwise it returns the first parameter. Listing 1 contains a NULLIF script that returns NULL for TestEqualNULLIF and 0 for TestNotEqualNULLIF. ISNULL checks for the value of a parameter and replaces NULL with another value if the parameter’s value is NULL. Listing 2 contains an ISNULL script that returns 1 for TestNULLValue and 10 for TestNotNULLValue.

Q: What’s the typical usage of NULLIF and ISNULL?

A: Use NULLIF to compare two values, and use ISNULL to compare a value with NULL. You can use CASE instead of NULLIF or ISNULL. However, using NULLIF or ISNULL reduces the amount of T-SQL script you need to write. Only a negligible performance difference exists between using NULLIF or ISNULL, or using CASE. Listing 3 demonstrates NULLIF and ISNULL, comparing each with an equivalent CASE statement. (Notice how much more code CASE requires.) Run the script in Listing 3 two times with two different values. The first time you run the script, set @expression2 to 0, and the second time set @expression2 to 1.

Q: What’s the difference between % used as an operator and % used as a wildcard?

A: When % is used as an operator, the modulo returns the remainder of a division operation. Using % as a wildcard character matches any length of the string or number in a WHERE clause. Listing 4 contains a query that uses % as modulo and as wildcard.



ARTICLE TOOLS

Comments
  • Christan
    4 years ago
    Feb 15, 2008

    Thanks for informing us about the problem, tilleymf@hotmail.com. The downloadable code should be available to you now.

    And thanks so much for reading!

    Christan Humphries, Your Savvy Assistant

  • MICHAEL
    4 years ago
    Feb 15, 2008

    Hi
    While reading the article "Sharpen Your Basic SQL Server Skills " I have attempted to save/open the "Download the Code" item 97337.zip and it appears to be empty. Tried this from multiple workstations, but to no avail.
    Thanks - Michael

  • Saleem
    5 years ago
    Dec 26, 2007

    Solution to pop-up ads: Stop playing with your pointer... The pop-up key words are green and double-underlined so aren't really "booby-traps".

    Eliminating pop-ups is a good way to increase the cost of everyone's subscription.

  • Robert
    5 years ago
    Nov 21, 2007

    Here's a comment from a new subscriber after two minutes of trying to look at my first "subscriber-only" article: I really hate those annoying fly-over links that pop up an irrelevant advertisement as I am trying to follow the text with my mouse pointer. I believe the fact that I've subscibed indicates that I paid for this content. Therefore, I do not understand why you feel compelled to plant advertising booby-traps in the paid content. Does anyone, including the advertiser imagine that inclination to purchase is increased by the use of these obnoxious pop-ups?

You must log on before posting a comment.

Are you a new visitor? Register Here