Replace Oracle sql with postgres sql - postgresql

how to replace bitand function and decode oracle function in postgres sql?
select decode(bitand(p.privilege, 2), 2, 'true', 'false') as is_approver
from person p;
tried below sql
select case ((p.privilege & 2 = 2))::int when 2 then 'true' else 'false' end as is_approver
from person p;

The expression p.privilege & 2 = 2 will return a boolean value. If you can work with them inside your application, then all you need is:
select (p.privilege::int & 2) = 2 as is_approver
from person p;
If you really want string values for true and false, then you need to test the boolean value. Your expression is comparing the boolean value to an integer
select case
when (p.privilege::int & 2) = 2 then 'true'
else 'false'
end as is_approver
from person p;

That should be
select (p.privilege::bigint & 2) = 2 as is_approver
from person p;

Related

Postgres CASE WHEN IN query

I've attempted the below query in postgres 11
CASE
WHEN planning_status::varchar in (('Application Under Consideration'::varchar,'Appeal In Progress'::varchar)) then 'this_label'
WHEN planning_status::varchar = 'Approved' and actual_completion_date is not null then 'that_label'
ELSE 'reject_label'
END
I can't get the query to run, initially getting error on mismatching operator to record type. I also attempted IN (VALUES()) method. The below works:
CASE
WHEN planning_status = 'Application Under Consideration' then 'this_label'
WHEN planning_status = 'Appeal In Progress' then 'this_label'
WHEN planning_status = 'Application Received' then 'this_label'
WHEN planning_status = 'Approved' and actual_completion_date is not null then 'that_label'
ELSE 'reject_label'
END
Is it possible to use the IN query within a CASE WHEN query with strings. The strings are categorical but not stored as such
The problem are the double parentheses:
-- this doesn't work:
SELECT CASE WHEN 1 IN ((1, 2)) THEN 'works' ELSE 'weird' END;
ERROR: operator does not exist: integer = record
LINE 1: SELECT CASE WHEN 1 IN ((1, 2)) THEN 'works' ELSE 'weird' END...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
-- this works:
SELECT CASE WHEN 1 IN (1, 2) THEN 'works' ELSE 'weird' END;
case
═══════
works
(1 row)
The reason is that in the first statement, the inner parentheses are forming a composite type (record) with two elements, and PostgreSQL doesn't know how to compare that to the integer 1.
If the = version works, then this should work:
(CASE WHEN planning_status IN ('Application Under Consideration', 'Appeal In Progress', 'Application Received')
THEN 'this_label'
WHEN planning_status = 'Approved' and actual_completion_date is not null
THEN 'that_label'
ELSE 'reject_label'
END)
There should be no need for an explicit type conversion. If this doesn't work, what is the type of planning_status?

Is possible change operator = by a variable inside a function POSTGRESQL?

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

Conditional / Optional where statement

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

need to translate specific t-sql case in pl/sql

Can anyone tell me how to translate the following T-SQL statement:
SELECT fileld1 = CASE
WHEN T.option1 THEN -1
ELSE
CASE WHEN T.option2 THEN 0
ELSE 1
END
END
FROM Table1 AS T
The point is I need to validate two different options from the table for a single field in the select statement..
I have tried to do somthing with an IF statement in pl/sql, but it just doesnt work for me:
SELECT IF T.option1 THEN -1
ELSE IF T.option2 THEN 0
ELSE 1
END
FROM Table1 AS T
I am not actually sure how to write IF statement inside the SELECT statement..
And also, I need to do it INSIDE the select statement because I am constructing a view.
Use:
SELECT CASE
WHEN T.option1 = ? THEN -1
WHEN T.option2 = ? THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
I can't get your original TSQL to work - I get:
Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.
...because there's no value evaluation. If you're checking if the columns are null, you'll need to use:
SELECT CASE
WHEN T.option1 IS NULL THEN -1
WHEN T.option2 IS NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
...or if you need when they are not null:
SELECT CASE
WHEN T.option1 IS NOT NULL THEN -1
WHEN T.option2 IS NOT NULL THEN 0
ELSE 1
END AS field1
FROM Table1 AS T
CASE expressions shortcircuit - if the first WHEN matches, it returns the value & exits handling for that row - so the options afterwards aren't considered.
If I remember correctly, PL/SQL also supports the case. You just would have to move the column alias from "field1=" before the expression to "AS filed1" after the expression.

How do I use T-SQL's Case/When?

I have a huge query which uses case/when often. Now I have this SQL here, which does not work.
(select case when xyz.something = 1
then
'SOMETEXT'
else
(select case when xyz.somethingelse = 1)
then
'SOMEOTHERTEXT'
end)
(select case when xyz.somethingelseagain = 2)
then
'SOMEOTHERTEXTGOESHERE'
end)
end) [ColumnName],
Whats causing trouble is xyz.somethingelseagain = 2, it says it could not bind that expression. xyz is some alias for a table which is joined further down in the query. Whats wrong here? Removing one of the 2 case/whens corrects that, but I need both of them, probably even more cases.
SELECT
CASE
WHEN xyz.something = 1 THEN 'SOMETEXT'
WHEN xyz.somethingelse = 1 THEN 'SOMEOTHERTEXT'
WHEN xyz.somethingelseagain = 2 THEN 'SOMEOTHERTEXTGOESHERE'
ELSE 'SOMETHING UNKNOWN'
END AS ColumnName;
As soon as a WHEN statement is true the break is implicit.
You will have to concider which WHEN Expression is the most likely to happen. If you put that WHEN at the end of a long list of WHEN statements, your sql is likely to be slower. So put it up front as the first.
More information here: break in case statement in T-SQL
declare #n int = 7,
#m int = 3;
select
case
when #n = 1 then
'SOMETEXT'
else
case
when #m = 1 then
'SOMEOTHERTEXT'
when #m = 2 then
'SOMEOTHERTEXTGOESHERE'
end
end as col1
-- n=1 => returns SOMETEXT regardless of #m
-- n=2 and m=1 => returns SOMEOTHERTEXT
-- n=2 and m=2 => returns SOMEOTHERTEXTGOESHERE
-- n=2 and m>2 => returns null (no else defined for inner case)
If logical test is against a single column then you could use something like
USE AdventureWorks2012;
GO
SELECT ProductNumber, Category =
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END,
Name
FROM Production.Product
ORDER BY ProductNumber;
GO
More information - https://learn.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql?view=sql-server-2017