Check for multiple values within CASE statement - oracle

How do I check for multiple things in my case statement without writing multiple lines, checking for each case.
I tried this and it's very ugly.
I want something clean like below but can't make it work.
select CASE
WHEN (UPPER(NAME) IN ('%AVG%', '%AVERAGE%') AND
FORMATTED_ENTRY NOT IN('<', '>'))
THEN FORMATTED_ENTRY END actual_avg
FROM VALUES_TABLE

Assuming that you want wildcard matching on those percent signs:
CASE WHEN (
( UPPER(NAME) LIKE '%AVG%' OR UPPER(NAME) LIKE '%AVERAGE%' )
AND FORMATTED_ENTRY NOT IN('<', '>')
)
THEN FORMATTED_ENTRY END actual_avg

Related

Spotfire: Can you write a case statement based on a column containing a substring

I want to create a calculated column based on a substring. I can't find the syntax to do something like the following
case
when [ProjectName] contains "substring" then [Value]
end
For example, when the [ProjectName] contains "overhead" then "overhead"
[ProjectName] would be equal to "Project 1 Overhead", "Project 2 billable", or something like that.
The easiest solution would be to use FIND() with an IF() or CASE() statement.
If(Find("overhead",Lower([ProjectName])) >0,[Value],"FALSE")
CASE
WHEN Find("overhead",Lower([ProjectName])) >0 THEN [Value]
END
Just remember, Find() is case sensitive.
If this is a step in replacing the sub-string with a value, then you would want to use RXReplace()

Regular Expression to match both first characters and last character in oracle

I have a table with a column with the structure:
Table name : re_result
res_id
--------------
PSI8765450
PSIRRRRTY781
ABCD000001
I want to fetch the values starting with PSI and ending with 1. My expected output is PSIRRRRTY781.
I am using query
Select * from re_result
Where regexp_like(^PSI*1)
But I am not getting the output. I am getting both PSIRRRRTY781 and ABCD000001.
Plz help
You do not need regular expressions; a simpler LIKE may do the work:
select res_id
from re_result
where res_id like 'PSI%1'
The same thing can be done with regexp:
where regexp_like(res_id, '^PSI(.*)1$')
This matches 'PSI' in the beginning of the string and '1' as last character, just before the end of string ($).
Here you find something more on regexp in Oracle
Another way to handle your query.
SELECT res_id FROM re_result WHERE UPPER(res_id) like UPPER('PSI%1')

Whats the XPath equivalent to SQL In query?

I would like to know whats the XPath equivalent to SQL In query. Basically in sql i can do this:
select * from tbl1 where Id in (1,2,3,4)
so i want something similar in XPath/Xsl:
i.e.
//*[#id= IN('51417','1121','111')]
Please advice
(In XPath 2,) the = operator always works like in.
I.e. you can use
//*[#id = ('51417','1121','111')]
A solution is to write out the options as separate conditions:
//*[(#id = '51417') or (#id = '1121') or (#id = '111')]
Another, slightly less verbose solution that looks a bit like a hack, though, would be to use the contains function:
//*[contains('-51417-1121-111-', concat('-', #id, '-'))]
Literally, this means you're checking whether the value of the id attribute (preceeded and succeeded by a delimiter character) is a substring of -51417-1121-111-. Note that I am using a hyphen (-) as a delimiter of the allowable values; you can replace that with any character that will not appear in the id attribute.

Oracle CASE in WHERE clause

Can someone help me with the below query in oracle?
The logic is that if the person has a friendlyname , use that for match with the 'search' criterion. Else, try to match with the realname column.
select * from people where
case when customer_friendlyname is null then realname like '%abcd%'
else
case when customer_friendlyname is not null then customer_friendlyname like '%abcd%'
end
end
Appreciated if someone could take a look.. Thank you!
In Oracle, Boolean expressions can't be treated like other types of expressions; for example, CASE expressions can't evaluate to them. So you need to rewrite this.
In this case, since you have the same LIKE '%abcd%' predicate in both branches, you could just factor it out:
WHERE ( CASE WHEN customer_friendlyname IS NULL
THEN realname
ELSE customer_friendlyname
END
) LIKE '%abcd%'
but it's simpler to make use of the built-in NVL function, and write:
WHERE NVL(customer_friendlyname, realname) LIKE '%abcd%'
SELECT *
FROM people
WHERE (customer_friendlyname LIKE '%abc%')
OR (customer_friendlyname is null and realname LIKE '%abc%')
You actually don't need the case here, this or clause will try the friendly name first it it was null it won't match, then it will try to match using the real name
You also can write it this way:
select * from people where
case
when customer_friendlyname is null and realname like '%abcd%'
then 1
when customer_friendlyname is not null and customer_friendlyname like '%abcd%'
then 1
else 0
end = 1
But it is more convenient in the case when you have more expressions.

addig char in select statement

I want to add char in Select statement.
Ex:
SELECT '.' + OUTTRUNK as NUMBER
Expected Result:
.348977834
.456935534
.090922834
.234999734
How can I do this?
Thanks.
Try the more SQL-ish:
SELECT '.' || OUTTRUNK as NUMBER
but keep in mind that, if OUTRUNK is a numeric rather than string type, you'll probably find that 090922834 will actually be 90922834 and will render as .90922834, not what you want.
If that's the case, you're probably looking for something more like:
SELECT OUTTRUNK / 1000000000 as NUMBER
(check the number of zeros there, I tried to get it right but testing is rightly your concern).

Resources