Case when function in oracle apex - oracle-apex-5

Im not sure what is the issue with this code. I need number to be changed based on Entity field
select CASE WHEN :P64_ENTITY = 'India' THEN 1
WHEN :P64_ENTITY = 'China' THEN 2
WHEN :P64_ENTITY = 'Japan' THEN 3
else 0 END;```

Most probably is your item :P64_ENTITY is empty in value property.

Related

how to filter data in sql

I am trying to filter the data according to several criteria, and based on these criteria, the record record number 7 (M08) should not appear because the "NewSiteID" column is not null and does not belong to the number 6, but the problem is that this record appears even though I used the conditions that appear in the query.
tbMachines.MachIsTransfered, tbMachines.NewSiteID
FROM tbMachines
WHERE BranchID = 6 AND MachIsTransfered = 0
OR BranchID = 6 AND MachIsBorrowed = 1
OR BranchID = 6 AND MachIsloaning = 1
AND NewSiteID = 6 OR NewSiteID = '' ```
[enter image description here][1]
[1]: https://i.stack.imgur.com/yhkUP.png
You need to use parenthesis around your conditions. In this case it can also be simplified quite a bit. If I understand what you are trying to do it could be something like this.
WHERE BranchID = 6
AND
(
MachIsTransfered = 0
OR
MachIsBorrowed = 1
OR
MachIsloaning = 1
)
AND NewSiteID in (6, '')

Update a column based on its current value

Is there any way to change field value based on its current value with one query?
Like I have tbl.team and if it's value = 1, change it to 2. And vice versa, tbl.team = 2 => 1.
You can use a case expression to update the column conditionally:
update the_table
set team = case
when team = 1 then 2
else 1
end
where team in (1,2);

Can a CASE expression have 2 resultant values

I have to write a case expression in SQL which goes like this,
case condition
if (T_CD = 'Y')
Case C_CD = 'H3'
set R_ID = 3 and RS_ID = 25
CASE A_FLG = 'N' and Mod = 'D'
set R_ID = 3 and RS_ID = 31
Both R_ID and RS_ID populate columns in a different table and have to be derived as per condition above.
My question is - Since I want 2 separate fields out of my case expression, will a single Case give out 2 resultant field values for me. Or Do I have to write 2 different case expressions for it.
If your dbms supports row types, maybe this works for you:
select case when a = 1 then (1,2) else (3,4) end from testtable;
The SQL Validator says:
The following feature outside Core SQL-2003 is used:
T051, "Row types"

Transform an Int "Id" to a Specific Text

i have a question, I want to display in a Datagrid in Flex some information of this Combobox: http://puu.sh/bU6ht/ee36ed7fd1.png
N/A = 0
B = 1
M = 2
NT = 3
FS = 4
ER = 5
SSE = 6
And is saved in the Database as int, but I want in the Datagrid display the information as String
http://puu.sh/bU6cR/3c112d85e3.png
I have a solution and is doing a Case in SQL, but i dont know if is the correct solution.
CASE insp._manguera
WHEN 0 THEN 'N/A'
WHEN 1 THEN 'B'
WHEN 2 THEN 'M'
WHEN 3 THEN 'NT'
WHEN 4 THEN 'FS'
WHEN 5 THEN 'ER'
WHEN 6 THEN 'SSE'
END as _manguera,
And do that for every field in the database
You could also have a table in which you could associate the values to letters and do a join on that table.
That way, you wouldn't need to repeat that code anywhere and you could always modify the table. Good for long time use and reuse.
tell me if you need an example :)

case compare to number and give out name

the problem I have is that I have a column called cate with the numbers 1 to 5 but I want alias names in the print out.
For example if the column has the number 1 I want STONE in the result set, if it is 2 I want "TREE".
I should look something like
Select
case when t.cate = 1 then t.cate="STONE"
case when t.cate = 2 then t.cate="TREE"
else null end as test from dbt.tbl t
I do not want to change the value in the table only in the print out.
Any idea how I can that to work?
Thanks for all your help in advance
remove extra case,
SELECT CASE WHEN t.cate = 1 THEN 'STONE'
WHEN t.cate = 2 THEN 'TREE'
ELSE null
END AS test
FROM dbt.tbl t
Alternatively, you can write
SELECT
CASE t.cate
WHEN 1 THEN 'STONE'
WHEN 2 THEN 'TREE'
ELSE NULL
END AS test
FROM dbt.tbl t
If the list is likely to change in the future (either through edits or additions), I'd do it as a separate table:
INSERT INTO Cates (Cate,Description) VALUES
(1,'Stone'),
(2,'Tree') --Etc
And then just do:
SELECT c.Description as Test
FROM dbt.tbl t inner join Cates c on t.Cate = c.Cate