missing right parenthesis when using if - sql

I am trying to use simple IF query in sql developer (Oracle), but i get 'missing right parenthesis' Error. I want to write all ID from my table TEST_TABLE, and write yes if ID is 1234, else 'no'. What am i doing wrong?
select id, if(id = 1234, 'yes', 'no') from TEST_TABLE t

Can't use IF there (IF is for control flow in PLSQL code, not queries), use CASE WHEN instead:
select id, CASE WHEN id = 1234 THEN 'yes' ELSE 'no' END as idIs1234 from TEST_TABLE t
The basic form of a case when is:
CASE
WHEN test THEN truevalue
[WHEN othertest THEN othertruevalue]
[ELSE falsevalue ]
END
CASE must do one or more tests that each return a single value. If no case matches (all tests are false) and there is no ELSE clause, null is returned.
There is also a form that tests a single variable for equality against various values:
CASE id WHEN 1234 THEN 'yes1' WHEN 2345 THEN 'yes2' ELSE 'no' END

Related

How to write case condition to check particular value exist among multiple values in sql?

I have a column in table which is having single/multiple value. I have to convert it yes/no based on condition.
example:
rendition_type_sys
distribution
uploaded, distribution
uploaded
single
I need to change the value based on condition. If column having distribution then value should convert as 'Yes' otherwise 'No'
Final Output:
rendition_type_sys
Yes
Yes
No
No
I tried one case statement but that is working for single value not multiple value-
case when ren.rendition_type__sys='distribution' then 'Yes' else 'No' end as rendition_type__sys
First, you should fix your data model so you are not storing multiple values in a string.
In the meantime, like should do what you want. For your example:
(case when ren.rendition_type__sys like '%distribution%'
then 'Yes' else 'No'
end) as rendition_type__sys
Note: In case "distribution" is part of an element name and you don't want that, you can check for delimiters. In Standard SQL, this would be:
(case when ', ' || ren.rendition_type__sys || ', ' like '%, distribution, %'
then 'Yes' else 'No'
end) as rendition_type__sys
The string concatenation operator may vary depending on the database you are using.
This below code would work assuming distribution is not part of any other discrete value; here is the code in a sample form:
WITH rt AS (
SELECT 'distribution' AS rendition_type_sys
UNION SELECT 'uploaded, distribution'
UNION SELECT 'uploaded'
UNION SELECT 'single'
)
SELECT CASE WHEN rt.rendition_type_sys LIKE 'distribution' THEN 'Yes' ELSE 'No' END AS Col
FROM rt
You might consider adding a table with for these "tags" so that they are discrete.

Operator 'is true' for type 'long' not found for oracle sql query

I am trying to run this logic, where i get output as follows,
My code to get the below output field is shown below
select max(dtm) over (partition by name ,id )-current_date from mm
output
-4168
-4168
-4168
-4127
what i want is to run this logic along with 'case when' statement so i tried:
case when max(dtm) over (partition by name ,id )-current_date then 'yes'
else 'No' end as output
from mm
but i get an error as follows, not sure what went wrong in this logic.
Operator 'is true' for type 'long' not found
There are two forms of CASE expression. One is referred to as simple case expression and the other is referred to as searched case expression. The SQL in your question uses the latter, i.e. searched case expression. I believe you probably need simple case expression, i.e.
select case max(dtm) over (partition by name ,id ) - current_date
when -4168 then 'Yes'
else 'No'
end as answer
from mm
select
case when current_date-max(dtm) over (partition by name ,id ) < 30 then 'yes'
else 'No' end as 'output'
from mm

If-Then Statement In A SQL Query Insists On Trying To Convert To Wrong Type, Then Fails

I have a SQL query, linking table 1 to table 2 via an inner join, containing this part in the select part of the statement:
select
table1.field1,
table2.field1,
CASE (table2.field1)
WHEN -2 THEN ''
ELSE table2.field2
END as table2Field2,
table3.field4
from...
I want to be able to return table2Field2 when it has a relevant value, ie: when the object represented in table2 is not null, so that table2.field1 does not have a value of -2. In this case, the value of table2Field2 should be blank instead of a meaningless value.
However, this returns 0 instead of the blank text. If I change this line:
WHEN -2 THEN ''
to this:
WHEN -2 THEN 'someText'
then it complains at me that it's trying to convert an int to a string, which I'm not. table2field1 is an int, but table2Field2 is a string, which is what we're actually returning here.
How do I state (even more specifically) in this query that I'm returning the string field as a string, and not something else as a string that isn't (a) a string, and (b) the thing I specified I'm returning please?
All suggestions welcome, many thanks in advance for any help :)
In a CASE expression, all of the possible return values must be of the same data type. As written, the expression is trying to return one string and one integer.
If you want an empty string for your first output, you can CAST or CONVERT your second output to a character type value:
select
table1.field1,
table2.field1,
CASE (table2.field1)
WHEN -2 THEN ''
ELSE CAST(table2.field2 AS varchar(12)) --< 12 will cover any value of integer.
END as table2Field2,
table3.field4
from...
Is it possible you have your as table2field2 in the wrong location?
maybe try:
select
table1.field1,
table2.field1,
CASE (table2.field1)
WHEN -2 THEN ''
ELSE table2.field2
END as table2Field2,
table3.field4
from...
Because you do not want to answer me what is the database you use then I have to do it like this hehehe:
SQL Server: DEMO
select
t.col1,
CASE
WHEN convert(char,t.col1) = '-2' THEN 'aaa'
ELSE convert(char,(t.col2))
END test
from Tab1 t;
Oracle DEMO
select
t.col1,
CASE
WHEN to_char(t.col1) = '-2' THEN 'aaa'
ELSE to_char(t.col2)
END test
from tab1 t;

What's the equivalent of ELSEIF in Apache hive?

I'm looking for a conditional structure in Apache Hive like the following Java code:
if (condition) value
elseif (condition) value
...
else value
Is there a way to do this in HiveQL?
You can use the case statement in HIVE
CASE Statement
The syntax for the case statement is:
CASE [ expression ]
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
WHEN conditionn THEN resultn
ELSE result
END
Here expression is optional. It is the value that you are comparing to the list of conditions. (ie: condition1, condition2, ... conditionn).
All the conditions must be of same datatype. Conditions are evaluated in the order listed. Once a condition is found to be true, the case statement will return the result and not evaluate the conditions any further.
All the results must be of same datatype. This is the value returned once a condition is found to be true.
IF no condition is found to be true, then the case statement will return the value in the ELSE clause. If the ELSE clause is omitted and no condition is found to be true, then the case statement will return NULL
Example:
CASE Fruit
WHEN 'APPLE' THEN 'The owner is APPLE'
WHEN 'ORANGE' THEN 'The owner is ORANGE'
ELSE 'It is another Fruit'
END
The other form of CASE is
CASE
WHEN Fruit = 'APPLE' THEN 'The owner is APPLE'
WHEN Fruit = 'ORANGE' THEN 'The owner is ORANGE'
ELSE 'It is another Fruit'
END

Return 'Yes' or No' from select statement?

tbl_LoanSummary has Sample_Number column. I have to check if Sample_Number column is not null the return 'Y' otherwise return return 'N' from below select statement.
select a.Br_Loan_No ,a.Br_LookupKey, //return IsNull(s.Sample_Number) ='N' or 'Y'
from dbo.tbl_Br a left outer join dbo.tbl_LoanSummary s
on s.Loan_no = a.Br_Loan_No order by a.Br_Loan_No
How to do this?
You can use the case expression for this...
select a.Br_Loan_No,
a.Br_LookupKey,
CASE WHEN s.Sample_Number IS NULL THEN 'N' ELSE 'Y' END AS [HasSample]
from dbo.tbl_Br a left outer join dbo.tbl_LoanSummary s
on s.Loan_no = a.Br_Loan_No order by a.Br_Loan_No
In Oracle, you could also use
select NVL(s.Sample_Number, 'N')
to return N in case of null value
(of course you still need something to have Y in case of not null.)
You'll want to use a CASE expression. It's like an embedded if-statement or switch-statement from traditional programming languages.
SELECT a.Br_Loan_No,
a.Br_LookupKey
CASE
WHEN s.Sample_Number IS NULL THEN 'N'
ELSE 'Y'
END AS sample_number_is_not_null
FROM dbo.tbl_Br a
LEFT JOIN dbo.tbl_LoanSummary s
ON s.Loan_no = a.Br_Loan_No
ORDER BY a.Br_Loan_no
Note that you are creating a computed column here, rather than selecting the raw value of an existing column. It's generally required that you give this column a name, thus the use of the AS sample_number_is_not_null.
There are two forms of the CASE expression. One lets you compare a column or value against several choices. It is like using an implicit equals:
CASE foo
WHEN 3 THEN 'foo is 3!'
WHEN 4 THEN 'foo is 4!'
ELSE 'foo is not 3 or 4'
END
The other form, in the example at the top, lets you use arbitrary expressions in each WHEN clause. It should be noted that each WHEN clause is evaluated in order and the first one to match is the one whose THEN is used as the result. If none of the WHENs match, then the result in the ELSE is used.