Checking for NOT NULL condition

  • I have a function where i have to check column not null

    if @sen like 'BY%' and @fdd NOT NULL

    begin
    select bla bla

    end

    else if @fdd NOT NULL

    begin

    select bla bla

    end

    Is the above check and format is correct?

  • You must use "IS NOT NULL" rather than just "NOT NULL":

    if @sen like 'BY%' and @fdd IS NOT NULL
    ...

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • And @fdd is a variable, not a column.  If you want to check that a column is not null, you'll need to put it in a WHERE (or possibly HAVING) clause of a SELECT/UPDATE/DELETE/INSERT statement.

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • mcfarlandparkway - Tuesday, January 24, 2017 9:34 AM

    I have a function where i have to check column not null

    if @sen like 'BY%' and @fdd NOT NULL

    begin
    select bla bla

    end

    else if @fdd NOT NULL

    begin

    select bla bla

    end

    Is the above check and format is correct?

    Have you considered using an inline table-valued function rather than a multistatement table-valued function or scalar function?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

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

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