Do we have any function in oracle do this scenario.
There are 2 columns in table col1, col2.
col1 either has 'Y' or 'N' or NULL
col2 eitehr has 'Y' or 'N' or NULL
But always only one of the column has value 'Y'.
So , i want to check first col1 whether it has 'Y' ,if it has i want a string "COL1 found" ,if col2 has 'Y' i want a string "COL2 found". Is it possible with NVL2 and Decode function. With one column ,i can able to do that. But here i have to check 2 columns .Please note that i want a result in single row.
Regards,
Chaitu
select case when col1 = 'Y' then 'Col1 found' when col2 = 'Y' then 'Col2 found' end f
from yourtable
UPD.
select decode (col1, 'Y', 'col1 found', decode(col2, 'Y', 'col2 found')) from yourtable
Related
I am trying to return all Col1 values IF the respective Col2 value = NULL
I have thought of IF Col2 = NULL Return Col2 END IF, But I am unsure of how to format this, because when I run it I get errors saying invalid relational operator. Thank you for your time.
SELECT Col1
FROM mytable
WHERE IF NULL Col2 = 0;
IF Col2 = NULL
RETURN COL1
ELSE
END IF;
================================================================
EDIT
I apologize for the lack of information/Show of research. First post, So I have been searching google for different IF then statements to deal with NULL values. Everything I have found this far has only showed how to Ignore/convert to 0 so that you can easily work with them. Nothing on how to use them to return values from another col1
use COALESCE. it resturns the first non null value
SELECT
COALESCE(col1,col2) AS FINAL_COLUMN
FROM [TABLE NAME]
SELECT Col1
FROM mytable
WHERE Col2 IS NULL
No IF is required. WHERE is the SQL IF for row selection. This returns only the rows where Col2 is null.
If you want to return all the rows, but want to return Col1 when Col2 is null and NULL otherwise, use
SELECT
CASE WHEN Col2 IS NULL THEN Col1 ELSE NULL END as myResultColumn
FROM mytable
I wish to create a table with many fields. And I need to do multiple case statement on each field. Please help me with a clean way (Function/Stored Proc) to do it, instead of writing case statement over and over.
Below is an example SQL.
SELECT
COALESCE(CASE WHEN Col1 = 'XXX' THEN 'YYY' ELSE NULL END,
CASE WHEN Col1 IN ('AAA','BBB') AND Col2 IS NULL THEN 'ZZZ' ELSE NULL END,
CASE WHEN Col1 = 'CCC' THEN 'DDD' ELSE Col2 END ) AS col_i
.
.
.
FROM table
Within a single query, you can use apply (or a subquery or CTE):
SELECT v.col_i
FROM table t CROSS APPLY
(VALUES (CASE . . . )
) v(col_i)
If you want this generally available, then don't use a function, use a computed column:
alter table t add col_i as
(case . . . );
This would then be available to anyone using the table.
I think we can rewrite your logic into a single CASE expression:
CASE WHEN Col1 = 'XXX'
THEN 'YYY'
WHEN Col1 IN ('AAA', 'BBB') AND Col2 IS NULL
THEN 'ZZZ'
WHEN Col1 = 'CCC'
THEN 'DDD'
ELSE Col2 END
What makes this work is that your ELSE values are NULL (except for the very last CASE expression), which logically means that COALESCE would just rollover to the next CASE expression.
I would like to get to know how I can get the first column with NULL value from the left for each row in my table, I've tried with SELECT CASE but it doesn't work the way I would like.
Guys, I'd like to be crystal-clear about what I want to accomplish. I have a table with 22 columns and there are rows in which last 10 columns have NULL values but I need to get to know only a name of the first column from the left with NULL value.
You get the value from the first non-NULL column using coalesce():
select coalesce(col1, col2, col3, . . .)
You can get the name using case logic:
select (case when col1 is not null then 'col1'
when col2 is not null then 'col2'
. . .
end)
Just specify NULL as your first field selection.
SELECT NULL, FieldA, FieldB, FieldC etc
FROM yourtable
The only general approach here is case statement:
Case
when col1 is null then 'col1'
when col2 is null then 'col2'
when col3 is null then 'col3'
end as frst_null
This way frst_null would contain the name of the first column containing Null value. You can order columns whichever order you like.
I have a table "table1" like
NAME COL1 COL2 COL3 COL4
A 1 2 3 4
I want to filter the table by
"SELECT * FROM table1 where (COL1=1 OR COL2=1 OR COL3=1 OR COL4=1)"
I expect the result is
NAME COL_NAME
A COL1
Is that possible by SQL statement?
You are not limited to selection of columns in your select list, you can use expressions. For example, you can use this:
SELECT
NAME, -- More columns as needed
CASE
WHEN COL1=1 THEN 'COL1'
WHEN COL2=1 THEN 'COL2'
WHEN COL3=1 THEN 'COL3'
WHEN COL4=1 THEN 'COL4'
ELSE NULL END AS COL_MATCH
FROM table1 where (COL1=1 OR COL2=1 OR COL3=1 OR COL4=1)
I have 2 fields in my table on which i need a case statement.
Col1 Col2 Col3
abc.txt Y 0
def.txt N 0
bbck.txt Y 1
The Col3 values are based on the Col1 and Col2 values in the following manner.
Y = 1 and N = 0. So all the values in Col2 that are Y shall become 1 in col3, and Nin Col2 will become 0 in Col3, UNLESS the col1 value ends with %c.txt. As you can see since the abc.txt ends with %c.txt the value in col3 becomes 0.
I know this can be done with a CASE statement nested maybe to get this done. Does anyone know how to?
here's my code
SELECT
CASE Col2
WHEN 'Y' THEN '1'
WHEN 'N' THEN '0'
ELSE
(CASE WHEN [Col1] LIKE '%c.txt' THEN '0'
END)
END
AS Col3,
*
FROM Tabl1
Hope this gives an idea
Maybe:
SELECT
CASE
WHEN Col2 = 'N'
OR Col1 LIKE '%c.txt'
THEN '0'
WHEN Col2 = 'Y'
THEN '1'
END AS Col3
, *
FROM Tabl1
SELECT
CASE WHEN Col2 LIKE '%c.txt' THEN '0'
ELSE
CASE
WHEN Col2 = 'N' THEN '0'
WHEN Col2 = 'Y' THEN '1'
END
END AS COL3
FROM Tabl1
I don't think your spec is clear enough e.g. is the comma before the word "UNLESS" suposed to incidicate that the following clause applies to both the previous clauses?
There seems to be four possible combinations:
1) Col1 LIKE '%c.txt'
AND Col2 = 'Y'
2) Col1 LIKE '%c.txt'
AND Col2 = 'N'
3) Col1 NOT LIKE '%c.txt'
AND Col2 = 'Y'
4) Col1 NOT LIKE '%c.txt'
AND Col2 = 'N'
You say
i need a case statement
Is this true? With SQL, the same thing can always be achieved multiple ways (which is why it is so hard to build a good optimizer :) I think a spec should state what is required and not place unreasonable restrictions on how to implement it.
The following uses UNION rather than case and if you had considered this from the start then maybe your spec would be better ;)
WITH Tabl1
AS
(
SELECT *
FROM (
VALUES ('abc.txt', 'Y'),
('def.txt', 'N'),
('bbck.txt', 'Y'),
('disc.txt', 'N')
) AS T (Col1, Col2)
)
SELECT '0' AS Col3, *
FROM Tabl1
WHERE Col1 LIKE '%c.txt'
AND Col2 = 'Y'
UNION
SELECT '0' AS Col3, *
FROM Tabl1
WHERE Col1 LIKE '%c.txt'
AND Col2 = 'N'
UNION
SELECT '1' AS Col3, *
FROM Tabl1
WHERE Col1 NOT LIKE '%c.txt'
AND Col2 = 'Y'
UNION
SELECT '0' AS Col3, *
FROM Tabl1
WHERE Col1 NOT LIKE '%c.txt'
AND Col2 = 'N';
P.S. I coded this to match the accepted answer but I'm not convinced it is correct!
This might be what your looking for:
SELECT * FROM Tabl1
cross apply
(
select Col3 =
CASE
WHEN Tabl1.Col2 = 'Y' then '1'
WHEN Tabl1.Col2 = 'Y' then '1'
WHEN RIGHT(Tabl1.Col1, 6) = 'c.txt' then '0'
END
) as Col3