Missing keyword ora -00905 - sql

I am getting the error indicated in the question title when I execute the following query:
select mantas_stg.NY_EGIFTS_TRANS_STG.*,
CASE WHEN MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_CURR != 'USD' THEN
CASE WHEN MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_CURR != 'USD' AND MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_LOCAL_CURR != 'USD' THEN
MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT = MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT * 100
END
END
from mantas_stg.NY_EGIFTS_TRANS_STG;
Can anyone tell me why the above query is failing at
MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT = MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT * 100

One doesn't assign values in a case expression. I think you want something like this:
select s.*,
(CASE WHEN s.FUNDS_ORIG_CURR <> 'USD' AND s.FUNDS_LOCAL_CURR <> 'USD'
THEN s.FUNDS_ORIG_AMT * 100
ELSE s.FUNDS_ORIG_AMT
END) as new_funds_orig_amt
from mantas_stg.NY_EGIFTS_TRANS_STG s;
Indentation and table aliases sure make the query easier to read.

The line below in your nested case is probably the issue:
THEN MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT = MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT * 100
You're trying to assign a value to MANTAS_STG.NY_EGIFTS_TRANS_STG.FUNDS_ORIG_AMT in a select statement which isn't allowed
If your goal is to update the value in that column of a particular record you should be using an update statement.

Related

'CASE' expression whether to apply a WHERE condition to a query or not

I've been trying to accomplish something like the code below inside a stored procedure
Select * from TABLE1
CASE WHEN #SPParameter != 0 THEN -- if #SPParameter equals 0 then apply the where condition
WHERE Table1Column = #SPParameter -- apply a where condition
END
This query's goal is to select all rows from TABLE1 if #SPParameter is equal to zero, otherwise filter rows from TABLE1 if #SPParameter is not equal to zero.
Obviously the query above would throw an error message since the syntax is incorrect. Is this possible? Or is an if else statement the only way out?
Just use simple boolean logic1:
Select * from TABLE1
WHERE #SPParameter != 0 OR Table1Column = somevalue
CASE is an expression. It computes a value. It doesn't arbitrarily rearrange the parse tree of the statement it appears in.
1It'll be slightly more complex if we have to deal with NULLs but I've ignored them for now.
You can achieve by using OR condition like this way
WHERE (#SPParameter = 0 OR Table1Column = somevalue)
SELECT Name, JobType
FROM EMP
WHERE 1 = CASE
WHEN JobType = 'VC' THEN 1
WHEN JobType = 'HR' THEN 1
WHEN JobType = 'DEV' THEN 1
ELSE 0
END;
Above example CASE returns if jobtype present in table then it will be return the Name and JobType.
You try using case in where something like below:
Select * from TABLE1
where Table1Column = case when #SPParameter = 0 then #SPParameter else #SPParameter end;
Select * from TABLE1
WHERE Table1Column = CASE
WHEN #SPParameter != 0 THEN #SPParameter
END;
Here WHERE condition gets the value from the CASE statement, if #SPParameter is not equals zero, which means the value is present THEN it will be return the #SPParameter value.

multiple values to oracle case statement then

Can some one please explain how to pass multiple values to oracle case statement Then
SELECT *
FROM impl_debitor_information
WHERE soft_delete='F'
AND SHOP_ID ='4987bc1b-c0a8-6cb7-12f4-0243011f7099'
AND (debitor_type IS NULL
OR debitor_type IN (CASE
WHEN (SELECT techfund_debitor_enabled
FROM impl_shop
WHERE shop_id='4987bc1b-c0a8-6cb7-12f4-0243011f7099') = 'YES' THEN ('T','D')
ELSE 'D'
END))
If values from
select techfund_debitor_enabled from impl_shop where shop_id='4987bc1b-c0a8-6cb7-12f4-0243011f7099' is "YES" then I need to pass 2 values to in clause, if not single value
Thanks in advance
CASE will only return a single value. You must rewrite your query. Something like this:
SELECT *
FROM impl_debitor_information i, impl_shop where shop_id s
WHERE d.soft_delete='F'
AND d.shop_id ='4987bc1b-c0a8-6cb7-12f4-0243011f7099'
AND d.shop_id = s.shop_id
AND (d.debitor_type IS NULL
OR (d.debitor_type IN ('T','D') AND s.techfund_debitor_enabled = 'YES')
OR (d.debitor_type IN ('D') AND s.techfund_debitor_enabled <> 'YES'))
There might be errors in it, I didn't test the query.

Case when statement in SQL

I am using the following query. In this query I want to apply the where clause based on passed parameter. But the issue is that where clause is like 'value = if parameterVal = 'I' than NULL else NOT NULL'
I've build a query like this
SELECT * FROM MASTER
WHERE
Column1 IS (CASE WHEN :Filter = 'I' THEN 'NULL' ELSE 'NOT NULL' END)
but it's not working. Help me solve this.
UPDATE
Updating question to elaborate question more clearly.
I've one table MASTER. Now I am passing one parameter in query that is Filter (indicated by :Filter in query).
Now when the Filter parameter's value is 'I' than it should return the following result.
SELECT * FROM MASTER WHERE Column1 IS NULL
but if the passed argument is not equal to 'I' than,
SELECT * FROM MASTER WHERE Column1 IS NOT NULL
SELECT * FROM MASTER
WHERE (Filter = 'I' AND Column1 IS NULL)
OR
(Filter <> 'I' AND Column1 IS NOT NULL)
If you really insist on using a CASE the SELECT could be rewritten as:
SELECT *
FROM MASTER
WHERE CASE
WHEN COLUMN1 IS NULL AND FILTER = 'I' THEN 1
WHEN COLUMN1 IS NOT NULL AND FILTER <> 'I' THEN 1
ELSE 0
END = 1
SQLFiddle here
Frankly, though, I think that this is very difficult to interpret, and I suggest that #MAli's version is better.
Your case has assignment not equality check

SQL Query - Return Text based on numeric value

I am trying to work out a query where the query will perform a count (total) on a specific column. If the count is greater than 0, I want to display YES and display NO if the returned count is zero.
So, if I a query as this:
SELECT COUNT(ProblemID)
FROM dbo.ProblemInfo
WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')
that will actually be a subquery, how do I get the subquery to display "YES" when the returned count is greater than 0 and NO if the count is 0?
I appreciate any insight and help.
select isnull(
SELECT MAX('YES')
FROM dbo.ProblemInfo
WHERE ProblemID IN (100,101,309,305,205,600,500)
AND DEPID = '10866'),
'NO')
This is a trick to return either YES if there's at least one matching row, or null if not.
The wrapping isnull call then turns a null into a NO
Here's an alternate way of querying that.
IF EXISTS(
SELECT *
FROM dbo.ProblemInfo
WHERE (ProblemID IN (100,101,309,305,205,600,500))
AND (DEPID = '10866')
)
BEGIN
SELECT 'Yes'
END
ELSE
BEGIN
SELECT 'No'
END
What I like about this method is that, for enormous data-sets, it should be noticeably faster.
try
SELECT CASE
WHEN (SELECT COUNT(ProblemID) FROM dbo.ProblemInfo WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')) > 0
THEN 'YES'
ELSE 'NO' END
FROM YourTable
you can use case when.
SELECT
case
when COUNT(ProblemID) = 0 then 'NO'
else 'YES'
end
FROM dbo.ProblemInfo WHERE (ProblemID IN (100,101,309,305,205,600,500)) AND (DEPID = '10866')

Case condition broken need assistance to fix

New to SQL, so please accept my apologies. A query was created that when
If HBL_CLNT_CAT._HS_EB_CODE1 = 'BF' then value = TBM_BILLGRP._HS_EB_DET1
If HBL_CLNT_CAT._HS_EB_CODE2 = 'BF' then value = TBM_BILLGRP._HS_EB_DET2
However of the _HS_EB_DET# exceeds 100 characters add a '*'.
With assistance a query was developed, however it broken the condition rules in that the 'then statement/action would fail because it was greater number than the condition statement (select _hs_eb_code1 from hbl_cat where hs_eb_code = 'bf' that returns only 1 record).
select
case when len(format) > 100
then left(format, 100) + '*'
else format
end as format
from
( select
case when exists ( select _hs_eb_code1
from hbl_cat
where hs_eb_code = 'bf'
)
then tbm_bllgrp._hs_eb_det1
end
) as format
from tbm_bllgrp
Formatting the code would have helped you find the error. Try this:
select
case when len(format) > 100
then left(format, 100) + '*'
else format
end as format
from
( select
case when exists ( select _hs_eb_code1
from hbl_cat
where hs_eb_code = 'bf'
)
then tbm_bllgrp._hs_eb_det1
end as format
from tbm_bllgrp
) as tmp
The above query is broken in several places. A working statement could look like this:
SELECT CASE
WHEN len(x.myval) > 100 THEN
left(x.myval,100) + '*'
ELSE
x.myval
END AS format
FROM (
SELECT CASE
WHEN h.HS_EB_CODE1 = 'BF' THEN
t._HS_EB_DET1
WHEN h._HS_EB_CODE2 = 'BF' THEN
t._HS_EB_DET2
ELSE
'unknown option'
END AS myval
FROM HBL_CLNT_CAT AS h
JOIN TBM_BILLGRP AS t ON ??? -- how are the two tables connected?
WHERE ??? -- some condition or do you want all rows in the table?
) AS x
But you need to tell us first, how TBM_BILLGRP and HBL_CLNT_CAT can be joined, and how you select your rows.
BTW, upper case is pointless in SQL-Server. Identifiers are case-insensitive as long as they are not enclosed in double quotes " " or brackets [ ].