Unix Technical Forum

postgresql wildcard when parameter is -1

This is a discussion on postgresql wildcard when parameter is -1 within the pgsql Novice forums, part of the PostgreSQL category; --> Hi guys, I'm still running into problems on my front end, but thats because of access, not postgres I ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 10:07 PM
ben sewell
 
Posts: n/a
Default postgresql wildcard when parameter is -1

Hi guys,
I'm still running into problems on my front end, but thats because of
access, not postgres

I have a question, when a parameter is recieved as -1 (as an integer from
Access), I would like that parameter to be a wildcard. The logic is that I
am sending the primary key for a table to postgres for a report but when
there hasnt been a value chosen in the combo box -1 is sent to postgres so
it would mean that that parameter don't apply in the SP, otherwise if it did
have another value then the parameter would be used to do some filtering
(joins).

Any ideas? Feel free to ask any questions if you want.

Cheers,
Ben

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 10:07 PM
Michael Fuhr
 
Posts: n/a
Default Re: postgresql wildcard when parameter is -1

On Thu, Aug 24, 2006 at 09:01:06AM +0100, ben sewell wrote:
> I have a question, when a parameter is recieved as -1 (as an integer from
> Access), I would like that parameter to be a wildcard. The logic is that I
> am sending the primary key for a table to postgres for a report but when
> there hasnt been a value chosen in the combo box -1 is sent to postgres so
> it would mean that that parameter don't apply in the SP, otherwise if it did
> have another value then the parameter would be used to do some filtering
> (joins).


You'll need to rewrite the query to handle the "wildcard". One way
would be with an expression like this:

WHERE (param = -1 OR param = column_name) AND ...

The parentheses are important if you have multiple expressions.

Another way would be to build the query string dynamically, adding
only the parts you need, then EXECUTE it (assuming PL/pgSQL; do the
equivalent in other languages). See "Executing Dynamic Commands"
in the PL/pgSQL documentation:

http://www.postgresql.org/docs/8.1/i...-EXECUTING-DYN

--
Michael Fuhr

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 10:08 PM
ben sewell
 
Posts: n/a
Default postgresql wildcard when parameter is -1

Hi Michael,
thanks for your reply. I've seen that % is the wildcard in postgres, so
couldnt I just do an if statement to overwrite the parameter? Also, I'm
interested in a wildcard for dates. Would that be %%/%%/%%%%?

Cheers,
Ben


On 8/24/06, Michael Fuhr <mike@fuhr.org> wrote:
>
> On Thu, Aug 24, 2006 at 09:01:06AM +0100, ben sewell wrote:
> > I have a question, when a parameter is recieved as -1 (as an integer

> from
> > Access), I would like that parameter to be a wildcard. The logic is that

> I
> > am sending the primary key for a table to postgres for a report but when
> > there hasnt been a value chosen in the combo box -1 is sent to postgres

> so
> > it would mean that that parameter don't apply in the SP, otherwise if it

> did
> > have another value then the parameter would be used to do some filtering
> > (joins).

>
> You'll need to rewrite the query to handle the "wildcard". One way
> would be with an expression like this:
>
> WHERE (param = -1 OR param = column_name) AND ...
>
> The parentheses are important if you have multiple expressions.
>
> Another way would be to build the query string dynamically, adding
> only the parts you need, then EXECUTE it (assuming PL/pgSQL; do the
> equivalent in other languages). See "Executing Dynamic Commands"
> in the PL/pgSQL documentation:
>
>
> http://www.postgresql.org/docs/8.1/i...-EXECUTING-DYN
>
> --
> Michael Fuhr
>


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 10:08 PM
Michael Fuhr
 
Posts: n/a
Default Re: postgresql wildcard when parameter is -1

On Fri, Aug 25, 2006 at 07:42:46AM +0100, ben sewell wrote:
> thanks for your reply. I've seen that % is the wildcard in postgres, so
> couldnt I just do an if statement to overwrite the parameter? Also, I'm
> interested in a wildcard for dates. Would that be %%/%%/%%%%?


The % character is a wildcard in text pattern matching; if you want
to use it to match non-text data then you'll have to cast that data
to text. Many common types have implicit casts to text so you might
be able to rely on that, although an explicit cast would make your
intentions more clear. However, now all your expressions would
have to use LIKE, which won't use non-text indexes. You could
address that by creating indexes on cast-to-text expressions, but
that has implications on storage space and performance of inserts,
updates, and deletes. You'll also have to consider whether pattern
matching instead of exact matching might yield unexpected results.

In pattern matching the % character matches zero or more characters,
so if your date output format is dd/mm/yyyy or mm/dd/yyyy then %/%/%
should work, or for yyyy-mm-dd then %-%-%. The underscore (_)
matches exactly one character, so you could also use __/__/____ or
____-__-__.

--
Michael Fuhr

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:31 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com