Using Decode as a like statement in oracle - oracle

I need to write a sql statement like this:
SELECT id_segmento AS Segmento, Decode (id_segmento , '1' , 'a', 'b' )
FROM mapchile.segmento
but in this case I will obtain an 'a' when id_segmento is equal to '1', I need it to be 'a' even when the string id_Segmento contains the '1', kind of like and like statment.
There is any other command like Decode that works this way?
Thanks.

I'd use a case statement. Something like
case
when id_segmento like '%1%' then 'a'
else 'b'
end

Use the CASE operator for complex evaluation instead of the DECODE function:
SELECT id_segmento AS Segmento,
CASE
WHEN id_segmento LIKE '%1%' THEN
'a'
ELSE
'b'
END
FROM mapchile.segmento

if you don't want to use case you can still use decode and instr:
decode(instr(id_segmento,'1'),0,'b','a')
I'm assuming you want to match on a '1' anywhere in the field. If you want to match on fields that start with a '1' then you could use:
decode(ascii(id_segmento),49,'a','b')
or
decode(substring(id_segmento,1,1),'1','a','b')
or
decode(instr(id_segmento,'1'),1,'a','b')

Related

Oracle/ PLSQL condition like 'string' || '%'

Why this
SELECT * FROM STUDENT
WHERE FULLNAME LIKE 'Nguyen' || '%'
as the same
SELECT * FROM STUDENT
WHERE FULLNAME LIKE 'Nguyen%'
How does the first one work?
|| is concatenation operator. Oracle will first perform concatenation and then will use LIKE to match the pattern. Hence operationally it will be same as the second one.
However you should use the second one as it will be more efficient in performance and easy to read.
First one has extra overhead to append two strings before using LIKE to match the pattern.
the first one catenate 'Nguyen' and '%' through the pipes '||' in first place.
Because you don't have any space like 'Nguyen ' or ' %', it's the same as 'Nguyen%'.
There is no difference between these two:
Here the double pipe(||) is just a concatenation of expression.
Before db evaluate against like parameter, it concatenates
Hence both of those are same.
Both are same, as || operator concats 'Nguyen' and '%', it will be more helpful if you want to concat parameterize variable like below
SELECT * FROM STUDENT
WHERE FULLNAME LIKE :name || '%'

Check for multiple values within CASE statement

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

Decoding with SUBSTRING and INSTRING?

I have a table which has city column having few records with state values as well-separated by comma.
There are other records without, as well. I want to take the state values for those present into a separate field called state.
How to do that? I tried the code below and it is saying "missing right parenthesis":
SELECT DECODE(ORA_CITY,
INSTR(ORA_CITY,',') > 0,
SUBSTR(ORA_CITY, INSTR(ORA_CITY, ','), LENGTH(ORA_CITY) ) ,
NULL) AS STATE
from ADDRESS
I don't know if you still need it but use CASE:
SELECT CASE
WHEN INSTR(ORA_CITY, '5') > 0 THEN
SUBSTR(ORA_CITY, INSTR(ORA_CITY, '5'), LENGTH(ORA_CITY))
ELSE
NULL
END STATE
FROM ADDRESS
Clearly you have not understood decode syntax.
Try the following:
SELECT DECODE(INSTR(ORA_CITY,','),
0,
NULL,
SUBSTR(ORA_CITY, INSTR(ORA_CITY, ','), LENGTH(ORA_CITY) )) AS STATE
FROM ADDRESS
The correct syntax is:
DECODE( expression , search , result [, search , result]... [,
default] ), where
expression is the value to compare.
search is the value that is compared against expression.
result is the value returned, if expression is equal to search.
default is optional. If no matches are found, the DECODE function will
return default. If default is omitted, then the DECODE function will
return null (if no matches are found).
Examples here and here
SELECT REGEX_REPLACE(ORA_CITY, '.*, *', '') AS STATE
FROM ADDRESS
WHERE ORA_CITY LIKE '%,%'
This uses regular expression to replace all upto the comma, and then maybe spaces with nothing. A WHERE included.

SQL - A simpler function than decode

I am working with a pl/sql procedure. I have an initialized variable myvar and I want to check its value : if it does not contain 'Z', I want it to contain 'P'.
I am currently doing it this way:
myvar := decode(myvar,'Z','Z','P');
I was just wondering if there was a simplier way to do this. I mean, decode is already simple, but I feel it's weird to specify the content of the variable while it is already in it !
If such a function would exist, it would look like this:
Function myfunction(a In Varchar2, b In Varchar2, c In Varchar2)
Return Varchar2
Is
Begin
if a <> b
then
return c;
end if;
return a;
End myfunction;
Any help would be appreciated !
There is no built-in function that does exactly what you want.
You could use CASE rather than DECODE:
CASE myvar WHEN 'Z' THEN 'Z' ELSE 'P' END
It doesn't make it any shorter though!
Put that function of yours to the program's declaration section and use it!
I agree the best option is to use CASE expression:
CASE myvar WHEN 'Z' THEN 'Z' ELSE 'P' END
Another approach if you feel happy with DECODE is to run this query:
SELECT decode(myvar,'Z','Z','P')
INTO myvar
FROM DUAL;
To answer your original question of whether there is a simpler way, there is also this:
if myvar <> 'Z' then
myvar := 'P'
end if;

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