Re: Query with Parameters and Wildcards

From: Mario Splivalo <mario(dot)splivalo(at)megafon(dot)hr>
To: landsharkdaddy <ldodd(at)landsharksoftware(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Query with Parameters and Wildcards
Date: 2009-04-27 11:40:59
Message-ID: 49F599CB.9000701@megafon.hr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

landsharkdaddy wrote:
> I have not tried that but I will in the morning. The @ in SQL is used to
> indicate a parameter passed to the query. In PostgreSQL it seems that the :
> is the same as the @ in SQL Server. I tried something like:
>
> SELECT * FROM Customers WHERE FirstName LIKE :custfirst + '%';
>
> And it told me that the + could not be used. Not sure the exact message but
> I will check again tomorrow and see what it was and post the results.

T-SQL defines that variables need to start with @ (like, for instance,
in PHP they star with $).

In postgres you have positional parametars, $1, for instance.

You could, for instance, write SQL function in postgres that would do
what you need:

CREATE FUNCTION get_customers_with_like (a_name_part character varying)
RETURNS SETOF customers
AS
$$
SELECT * FROM customers WHERE firstname LIKE $1 || '%';
$$
LANGUAGE 'sql';

In postgres, you use '||' for string concatenation (instead of '+' in
T-SQL).

Mario

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message landsharkdaddy 2009-04-27 14:19:47 Re: Query with Parameters and Wildcards
Previous Message Jasen Betts 2009-04-27 08:53:49 Re: varchar value comparisons not working?