#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    warsaw, poland
    Posts
    15
    Rep Power
    0

    check if array is empty and if value is in array


    i pass some array integer[] arguments to my function. inside, i'd like to check if the passed array isn't empty, and then build a clause to check if the selected values are included in this array.

    "brand_types" below is an alias for an integer[] argument
    Code:
              if (brand_types not null) then
                 wherestring := wherestring || ' AND brand_id IN ' || brand_types ;
              end if;
    it gives me a parse error on 'not null'
    what are the plpgsql synonyms for (for example) the php empty($array), and in_array($value, $array) ?

    the field is an integer, but the argument is an array. must check if the field value is in this array.. is it possible without looping and adding "and brand_id=array[n]" (when the array is huge, the generated query would be enormous) :/ ?


    maybe let's make the question easier:
    Code:
    select * from ads_all where id>0 and brand_id = array[1,2,3,4,5];
    what sould be instead of "=" in order to make this query work? "IN" doesn't work either
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    warsaw, poland
    Posts
    15
    Rep Power
    0
    ok, i solved the problem myself once more i already like this forum, just as i post, i solve the problem myself

    so, the proper expression is
    ... where brand_id in (1,2,3,4)

    so i connect the array values using array_to_string(brand_types, ',')

    the null problem i solved checking the first array's value, but if sbdy knows a better idea, i'll be gratefull
  4. #3
  5. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635
    It's always great when you can find the answers. It's even better when people post the results so others searching can get help also.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    warsaw, poland
    Posts
    15
    Rep Power
    0
    u know, when it's going about f.ex. programming in php, answer to such questions is 'rtfm', but with postgres i'm disgusted about the documentation :/ i search dozens of pages and have a book and still don't know how to do many things :/
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    373
    Rep Power
    12
    Originally Posted by zalew
    u know, when it's going about f.ex. programming in php, answer to such questions is 'rtfm', but with postgres i'm disgusted about the documentation :/ i search dozens of pages and have a book and still don't know how to do many things :/
    That's why I like that it is open source. grep is your friend...

IMN logo majestic logo threadwatch logo seochat tools logo