I don't know if this is possible, but this is the question. I try to change operator = by > if paramvalue = 0
AS $BODY$
declare
operator text;
begin
operator:='=';
if (paramvalue = 0) then
operator:='>';
end if;
select * from tablaname where id #operator 20
thanks!
I think overloading an operator is more complex than what you need for this behavior. You could try using a CASE statement instead.
SELECT *
FROM tablename
WHERE CASE WHEN paramvalue = 0
THEN id > 20
ELSE id = paramvalue
END
;
If you really want to overload an operator I suggest taking a look at the postgres documentation here.
REMARK NOT TESTED !!
In your scenario solution could be...:
.....
if (paramvalue = 0) then
select * from tablaname where id > 20
Else
select * from tablaname where id = 20
end if;
Please read this 9.16. Conditional Expressions
And this would be rather:
CASE WHEN paramvalue = 0 THEN select * from tablaname where id > 20
ELSE select * from tablaname where id = 20
END
Related
can you tell me how to use CASE and IFNULL in postgre? i'm migrating from sql to postgreSQL, and i want to make a function with this syntax :
SELECT a.furniture_id, a.furniture_brand, a.furniture_cost,
CASE
WHEN furniture_id = f_id THEN a.furniture_content
ELSE ''
END CONTENT
FROM tb_furniture a
WHERE
(IFNULL(a.sale_id,0) = IFNULL(f_id,0) OR (a.furniture_id = f_Id AND IFNULL(a.furniture_content,'') > ''))
AND a.published = 1;
thanks before(y)
This seems to be what you are after, but carefully check the WHERE conditions:
SELECT a.furniture_id, a.furniture_brand, a.furniture_cost,
CASE WHEN a.furniture_id = f_id
THEN a.furniture_content
ELSE ''
END AS content
FROM tb_furniture a
WHERE (coalesce(a.sale_id,0) = coalesce(f_id,0) OR length(content) > 0)
AND a.published = 1;
I have problem multiplying two host variables in embedded SQL for PostgreSQL.
The SQL-query is big but I have cut out the part that doesn't work.
Declaration:
EXEC SQL BEGIN DECLARE SECTION;
int var1;
int var2;
EXEC SQL END DECLARE SECTION;
Code:
CASE WHEN product_id = :var1 THEN :var1 * :var2
ELSE 0 END
The compilation works but I get the following error message at execution:
Errcode: -400
Errmsg: operator is not unique: unknown * unknown on line 1394
If I change the code to
CASE WHEN product_id = :var1 THEN 1 * :var2
ELSE 0 END
or
CASE WHEN product_id = :var1 THEN product_id * :var2
ELSE 0 END
or
CASE WHEN product_id = :var1 THEN :var1 * (1 * :var2)
ELSE 0 END
it works.
Is it possible to multiply two host variables? If not, is there any workaround? The last code example above works but I would like a solution that is not as ugly.
Try casting to integer:
CASE WHEN product_id = :var1 THEN :var1::integer * :var2::integer
ELSE 0 END
The error
operator is not unique...
can mostly be fixed by casting to the expected type
I have the following decitiontree:
declare #placeholder varchar(20)
If #Number1 = 1
AND #Number2 = 0
BEGIN SET #placeholder = 'NULL'
END
ELSE IF #Number1 = 1
AND #Number2 > 0
BEGIN SET #placeholder = Between (#Number2*10) AND (#Number2*10+9)
END
ELSE
BEGIN
SET #placeholder = #Othervariable
END
I need the Variable for the query:
SELECT * FROM Table
WHERE #Placeholder is null or ID = #placeholder.
But the 'Between' part is not working. Can anyone help me with it?
This won't work in SQL server - there is no variable type that holds something like lambda expression, say Between (#Number2*10) AND (#Number2*10+9).
One way is to store this in string (say, nvarchar(max)) and execute using exec() or sp_executesql().
The other (usually more optimized) way is to form the main expression to include sub-expressions along with exclusion criteria.
Here is an example:
DECLARE #Number1 int = 1 -- input variable 1
DECLARE #Number2 int = 1 -- input variable 2
DECLARE #excl1 bit = 0 -- exclusion criteria 1 (ec1)
DECLARE #excl2 bit = 0 -- exclusion criteria 2 (ec2)
-- fill excl. crit.
SET #excl1 = CASE WHEN #Number1 = 1 AND #Number2 = 0 THEN 1 ELSE 0 END
SET #excl2 = CASE WHEN #Number1 = 1 AND #Number2 > 0 THEN 1 ELSE 0 END
-- just output to see what's happening
PRINT #Number1
PRINT #Number2
PRINT #excl1
PRINT #excl2
SELECT *
FROM Table
WHERE
-- if ec1 is active, we apply sub-expression 1
(#excl1 = 0 OR
(#excl1 = 1 AND ID IS NULL))
AND
-- if ec2 is active, we apply sub-expression 2
(#excl2 = 0 OR
(#excl2 = 1 AND ID BETWEEN #Number2 * 10 AND #Number2 * 10 + 9))
I have a stored procedures and a few boolean variables (Bit).
I want to put a WHERE statement if a certain variables is False.
I know I could do something like this :
IF (#myBoolean = 1)
BEGIN
SELECT * FROM myTable
END
ELSE
SELECT * FROM myTable WHERE myTable.Foo = 'Bar'
Is there a way to make the WHERE statement optionnal ? Because I have so many boolean variable I don't want to have a different query for each possibilities.
This is what I have in mind (I know it does not work) :
SELECT * FROM myTable
CASE WHEN #myBoolean = 0
THEN WHERE myTable.Foo = 'Bar'
ELSE --Do nothing
SELECT * FROM myTable
where ( #myboolean = 1 and foo = 'bar' ) or #myboolean = 0
Could anyone help me write up an If Else statement for iPhone's SQLite?
I need a:
IF (SELECT COUNT (*) FROM ..... WHERE ... = '..') > 0
BEGIN
SELECT * FROM .... WHERE ... = '...'
END
But I cannot get it to work. Is it even possible with SQLite.
Thanks for any help!
SELECT
*
FROM
....
WHERE
... = '...'
AND (SELECT COUNT (*) FROM ..... WHERE ... = '..') > 0