Oracle sql case when exclude cells containg string - sql

I have this sql query which works well:
SELECT distinct
A,
CASE
WHEN Parameter = 'ID' and regexp_like (VALUE, 'FOO')
THEN VALUE
ELSE 'NA'
END AS test FROM my_table;
my_table
A
parameter
value
x
ID
FOO1223
y
ID
FOO5462
z
ID
empy
p
ID
BAR5432
result:
A
value
x
FOO1223
y
FOO5462
z
NA
p
NA
Now I would like to exclude VALUE that starts with 'BAR'. How can I add this to the CASE...WHEN statement?
The output should look this:
A
value
x
FOO1223
y
FOO5462
z
NA

Do NOT LIKE in the WHERE clause to skip the rows where value start with BAR.
SELECT distinct
A,
CASE
WHEN Parameter = 'ID' and regexp_like (VALUE, 'FOO')
THEN VALUE
ELSE 'NA'
END AS test FROM my_table
WHERE value NOT LIKE 'BAR%'

Related

Compare nulll value with non null value SQL

I have two columns as below:
Column A
Column B
A1
NULL
A1
A1
B1
C1.
When i query these columns as below :
SELECT Column A, Column B
from table
where Column A != Column B
i am expecting the following result:
Column A
Column B
A1
NULL
B1.
C1
But my query is only giving me the second line as result.
Consider below
select *
from your_table
where columnA is distinct from columnB
with output
Null values do not participate in equality operations as there is no value to compare with.
You will need to include a check for null into your comparisoon statement.
SELECT Column A, Column B from table where ISNULL(Column A,0) <> ISNULL(Column B,0)
null in SQL equals unknown
It could also have A1 as value, therefore it will not show up if you check if A1 is not null
You can use below workaround
select * from your_table
except distinct
select * from your_table
where columnA = columnB
with output
SELECT * from Your_table where IFNULL(`Column A`, 'NULL') != IFNULL(`Column B`, 'NULL')
IFNULL(expression, alt_value) function returns the value of the expression if it's not NULL and returns alt_value when expression is NULL.
So, from the above query, Column A and Column B will have the string value of 'NULL' when the column has the NULL value so they can still be comparable.

Is there a way to create a new column and assign values based on criteria on an existing column in a SQL query?

I want to create a new column in my query output and assign values to it based on existing columns in my input. Let's say i have column A in my input and want to create column B in my query output. How do I add the following logic in a sql select statement?
If A = 1 then set B to x
Else if A = 2 then set B to y
Else if A = 3 then set B to z
Else set B to null
I have to do this for multiple new columns in my output within the same query.
This is a case expression:
select . . .,
(case A when 1 then 'x' when 2 then 'y' when 3 then 'z' end) as b
Something like this?
SELECT A,
CASE
WHEN A = 1 THEN 'x'
WHEN A = 2 THEN 'y'
ELSE 'z'
END AS B
FROM ...

SQL Update all rows for one column with a list of values

How can I set all rows of a column by manually typing the values?
My table has 3 rows, I want the column named mycolumn to have 3 values a, b and c (currently those values are NULL):
update mytable set mycolumn = ('a','b','c')
ORA-00907 missing right parenthesis
EDIT: my table is very simple, I have one column ID INT NOT NULL with values 1, 2, 3 and another column mycolumn with all NULL values and I want those values to become 'a' where ID = 1, 'b' where ID=2 etc.
EDIT2: I might have a huge amount of rows, so I want to avoid typing every single ID value where to replace mycolumn. Isn't it possible to match the ID values of 1 to 3 to the values 'a', 'b', 'c' in an automatic way, something like match(ID, ('a','b','c')) perhaps
I just want to replace all values of mycolumn by increasing order of ID. ID being strictly equivalent to what I call a row number in a matrix
EDIT3: I'd like a solution which would work in a general case with all sorts of values, not only the letters of the alphabet given here for simplicity. What if for example my values to replace in mycolumn are ('oefaihfoiashfe', 'fiaohoawdihoiwahopah', 'aefohdfaohdao')? However the ID row numbers will always be a sequence from 1 to N by 1.
Obviously, you should do this in a single update. Like this:
update mytable
set mycolumn = case id when 1 then 'a' when 2 then 'b' when 3 then 'c' end
;
More compact (but also more cryptic, and only works in Oracle, while case expressions are in the SQL standard):
update mytable
set mycolumn = decode(id, 1, 'a', 2, 'b', 3, 'c')
;
Note - this only works if there really are only three rows. If you have many more rows, make sure to add where id in (1, 2, 3) at the end. Otherwise all the OTHER values (in the other rows) will be updated to null!
You can try an update like the one below. This will update 1 > a, 2 > b, 3 > c, 4 > d, etc. When you reach ID 27, since there are no more letters, it will begin at a again and continue down the alphabet.
UPDATE mytable
SET mycolumn = CASE MOD (id, 26)
WHEN 0 THEN 'z'
ELSE CHR (MOD (id, 26) + 96)
END;
Update
To update based on any list of values, you can try an update statement like the one below. If you add a 4th item to the comma delimited list, ID 4 in mytable will be set to whatever you specified as the 4th value.
UPDATE mytable
SET mycolumn =
(SELECT COLUMN_VALUE
FROM (SELECT ROWNUM AS row_num, t.COLUMN_VALUE
FROM TABLE (
sys.odcivarchar2list ('oefaihfoiashfe',
'fiaohoawdihoiwahopah',
'aefohdfaohdao')) t)
WHERE row_num = id);
Hmmm . . . A row can only have one value. Perhaps something like this to assign random values:
update mytable
set mycolumn = (case floor(dbms_random.random * 3)
case 0 then 'a' case 1 then 'b' else 'c'
end)
if you want the 3 rows to have different values a, b and c then you will have to write 3 update statements.
update mytable set mycolumn = 'a' where id = 1;
update mytable set mycolumn = 'b' where id = 2;
update mytable set mycolumn = 'c' where id = 3;

nvl with query inside

Its possible to do something like this:
and zmt.mediatypeid in nvl(:P21_MEDIA, select mediatypeid from zbx.media_type)
:P21_MEDIA is a variable
Yes, it is called a scalar subquery expression:
select x, (select y from another_table where foo = x) from the_table
select x from the_table where foo = (select y from another_table where bar = x)
It can only return a single column and a single row, anything else is an error.
I cannot test it right now, but I would assume you can also use it inside a function such as NVL. The documentation only mentions that it cannot be used in GROUP BY.
yes, you can use CASE statement to check the condition:
and 1 = CASE
WHEN :P21_MEDIA IS NOT NULL THEN
CASE
WHEN zmt.mediatypeid = :P21_MEDIA THEN 1
ELSE 0
END
WHEN zmt.mediatypeid IN (select mediatypeid from zbx.media_type) THEN 1
ELSE 0
END

Set Values in column B based on values in Column A in SQL2005?

I'm trying to take an existing column and parse each row for certain words in a string, i.e. Sheet, Page, Card, and based on that word (only one instance of one of these words is in a row) populate a new column in the same table with a value. If it did not find any of those words, leave Column B blank.
I.E. Column A contains the word "Sheet", populate Column B with the letter "S"
So the table would be something like:
Column A Column B
Sheet S
Page P
Card C
Any help would be appreciated!
UPDATE YourTable
SET ColumnB = CASE WHEN ColumnA LIKE '%Sheet%' THEN 'S'
WHEN ColumnA LIKE '%Page%' THEN 'P'
WHEN ColumnA LIKE '%Card%' THEN 'C'
ELSE NULL /* or '' if you prefer */
END
;WITH mappings (ColumnA, ColumnB) AS
(
SELECT 'Sheet','S' UNION ALL
SELECT 'Page','P' UNION ALL
SELECT 'Card','C'
)
UPDATE y
SET y.ColumnB= m.ColumnB
FROM YourTable y
JOIN mappings m ON y.ColumnA LIKE '%' + m.ColumnA + '%'
update myTable
set columnB = substring(columnA, 1, 1)