Results 1 to 14 of 14
  1. #1
    Join Date
    Nov 2009
    Posts
    6

    Unanswered: Where clause to select both null and not null rows, based on input parameter

    Is there a way to select a row based on a column that can be both null and not null?

    declare @parCat as int ;
    declare @name as nvarchar(255);

    set @parCat = 1;
    --set @parCat = null
    set @name = 'test1';

    INSERT INTO category(idParCat,name,sortOrder, publish, typeOfData)
    SELECT @parCat, @name, MAX(sortOrder)+1, 'TRUE', NULL FROM category
    WHERE idParCat = @parCat
    Last edited by isvaljek; 11-19-09 at 09:44.

  2. #2
    Join Date
    Dec 2007
    Location
    Richmond, VA
    Posts
    1,328
    Provided Answers: 5
    is null is not null?

  3. #3
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    I'm afraid I don't understand. Can you provide some sample data and expected output?
    George
    Home | Blog

  4. #4
    Join Date
    Nov 2009
    Posts
    6
    Quote Originally Posted by gvee View Post
    I'm afraid I don't understand. Can you provide some sample data and expected output?
    The category table has self-referencing structure, like this:

    tblCategory
    -idCat [int, not null] PK
    -idParCat [int, null] FK
    -name

    idCat, idParCat, name
    ---------------------
    1, null, 'trees'
    2, null, 'animals'
    3, 2, 'mammals'
    4, 2, 'birds'
    5, 4, 'colibri'
    ...

    What I would like is to pass a @parCat(parent category) parameter to select query that can be either int or null value, and that query should select the category rows with idParCat equal @parCat. But if I pass null as a parameter then the where clause idParCat = @parCat returns nothing instead of rows containing null(like top level categories, trees and animals).
    If i pass an integer it works.
    I would need to check for both "idParCat = @parCat" and "@parCat is NULL".

  5. #5
    Join Date
    Nov 2009
    Posts
    2
    Quote Originally Posted by isvaljek View Post
    The category table has self-referencing structure, like this:

    tblCategory
    -idCat [int, not null] PK
    -idParCat [int, null] FK
    -name

    idCat, idParCat, name
    ---------------------
    1, null, 'trees'
    2, null, 'animals'
    3, 2, 'mammals'
    4, 2, 'birds'
    5, 4, 'colibri'
    ...

    What I would like is to pass a @parCat(parent category) parameter to select query that can be either int or null value, and that query should select the category rows with idParCat equal @parCat. But if I pass null as a parameter then the where clause idParCat = @parCat returns nothing instead of rows containing null(like top level categories, trees and animals).
    If i pass an integer it works.
    I would need to check for both "idParCat = @parCat" and "@parCat is NULL".
    where (@parCat is null and parCat is null) or (@parCat is not nul and @parCat = parCat)?

  6. #6
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    WHERE idParCat = COALESCE(@parCat,idParCat )



    ????
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  7. #7
    Join Date
    Dec 2007
    Location
    Richmond, VA
    Posts
    1,328
    Provided Answers: 5
    how about a union? On one side "idParCat = @parCat" and on the other "@parCat is NULL"

  8. #8
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Quote Originally Posted by dav1mo View Post
    how about a union? On one side "idParCat = @parCat" and on the other "@parCat is NULL"
    Have a look at, and try my solution
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  9. #9
    Join Date
    Nov 2009
    Posts
    6
    Quote Originally Posted by Brett Kaiser View Post
    WHERE idParCat = COALESCE(@parCat,idParCat )

    ????
    I have tried this solution and it didn't work. COALESCE returns first non-null value inside parenthesis, right?
    Last edited by isvaljek; 11-20-09 at 14:15.

  10. #10
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    what does not work about it? that should totally work.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  11. #11
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Quote Originally Posted by isvaljek View Post
    I have tried this solution and it didn't work. COALESCE returns first non-null value inside parenthesis, right?
    Right

    This massage is too short
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  12. #12
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Code:
    INSERT INTO category(idParCat,name,sortOrder, publish, typeOfData) 
       SELECT @parCat, @name, MAX(sortOrder)+1, 'TRUE', NULL
          FROM category
          WHERE idParCat = @parCat OR (@parCat IS NULL AND idParCat IS NULL)
    -PatP
    In theory, theory and practice are identical. In practice, theory and practice are unrelated.

  13. #13
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    picky picky

    Code:
    WHERE idParCat = COALESCE(@parCat,idParCat )
    OR idParCat IS NULL
    Pat is saying Null is not equal to anything...even itself

    it being an existential thing here
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  14. #14
    Join Date
    Nov 2009
    Posts
    6
    Quote Originally Posted by shibormot View Post
    where (@parCat is null and parCat is null) or (@parCat is not nul and @parCat = parCat)?
    Thanks, that did it!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •