SQL Server Case in Where clause - sql

I am trying to create a stored proc and have a where clause where to different operations can take place depending on the value of a parameter passed in:
WHERE
(cdr.CircuitReference = #CircuitReference)
AND
CASE WHEN (#JDEDocumentReference <> 'Unbilled Calls')
THEN
sct.JDEDocumentReference = #JDEDocumentReference
ELSE
((sct.JDEDocumentReference IS NULL) AND (sc.StartDate IS NOT null AND ((sc.CloseDate IS null) OR (datediff(day,sc.CloseDate,getdate()) < 0)) AND stp.SipTrunksProduct = sct.ProductCode))
END
I've just posted my where clause above but when i try to execute the script i get the following error:
Incorrect syntax near '='.
Is this the correct way to do a conditional statement in a where clause of an sql query?
Thanks

This problem could be solved without a CASE statement by using the following:
WHERE
(cdr.CircuitReference = #CircuitReference)
AND
((#JDEDocumentReference <> 'Unbilled Calls' AND sct.JDEDocumentReference = #JDEDocumentReference)
OR
(#JDEDocumentReference = 'Unbilled Calls' AND ((sct.JDEDocumentReference IS NULL) AND (sc.StartDate IS NOT null AND ((sc.CloseDate IS null) OR (datediff(day,sc.CloseDate,getdate()) < 0)) AND stp.SipTrunksProduct = sct.ProductCode))))

STATEMENT FULLY WRONG : There is no need for case here(Even there is a possibility to it correctly. But here no needed).
USE:
(cdr.CircuitReference = #CircuitReference)
AND ((JDEDocumentReference <> 'Unbilled Calls'
AND #JDEDocumentReference) OR #JDEDocumentReference = 'Unbilled Calls' )
OR (JDEDocumentReference = 'Unbilled Calls'
AND ((sct.JDEDocumentReference IS NULL) AND (sc.StartDate IS NOT null AND ((sc.CloseDate IS null)
OR (datediff(day,sc.CloseDate,getdate()) < 0)) AND stp.SipTrunksProduct = sct.ProductCode)))

You can use something like this,
WHERE
(cdr.CircuitReference = #CircuitReference)
AND sct.JDEDocumentReference = case when #JDEDocumentReference <> 'Unbilled Calls' Then #JDEDocumentReference end

Related

Oracle Apex - Case within a where statement

I'm having issues while running the following query (interactive report / simplified the query cause I'm sure the issue is with the case statement):
select
v.manager_email
from my_table v
where
(
case
when :P28_ACT_AS_ROLE_H = 'Director' then v.director_email = :P28_ACT_AS_H
when :P28_ACT_AS_ROLE_H = 'Admin' then v.manager_email = nvl(:P28_ACT_AS_H,
v.manager_email)
when :P28_ACT_AS_ROLE_H = 'Manager' then v.manager_email = :P28_ACT_AS_H
end
)
The error is this one: ORA-20999.
Does someone know why this is happening?
(:p28 items are calculated via computations and work perfectly)
Thanks!
Don't use a case when boolean logic suffices:
where (:P28_ACT_AS_ROLE_H = 'Director' and v.director_email = :P28_ACT_AS_H) or
(:P28_ACT_AS_ROLE_H = 'Admin' and v.manager_email = nvl(:P28_ACT_AS_H, v.manager_email)) or
(:P28_ACT_AS_ROLE_H = 'Manager' and v.manager_email = :P28_ACT_AS_H)
The specific reason in your example is that Oracle doesn't recognize a boolean expression as a valid return value for a case, resulting in a syntax error
Or, with CASE:
where nvl(:P28_CT_AS_H, v.manager_email) =
case when :P28_ACT_AS_ROLE_H = 'Director' then v.director_email
when :P28_ACT_AS_ROLE_H = 'Admin' then v.manager_email
when :P28_ACT_AS_ROLE_H = 'Manager' then v.manager_email
end

What is the Purpose of the IsNull in this query?

I have a desktop application that I am converting to web and I am having trouble understanding the purpose of the IsNull parts of the query. The query is for Ms SQL and I know it has a IsNull function but this is not it. So I'm confused as to it's purpose. Below is my query:
UPDATE tb_category
SET
Email = #Email,
CandidateID = #CandidateID,
Code = #Code,
TestDate = #TestDate,
Description = #Description,
PointsEarned = #PointsEarned,
PointsAvailable = #PointsAvailable,
Average25th = #Average25th,
Average75th = #Average75th,
ImportedDate = #ImportedDate,
CreationDate = #CreationDate,
TestNum = #TestNum,
CategoryNum = #CategoryNum
WHERE ((Email = #Original_Email)
AND (CandidateID = #Original_CandidateID)
AND (Code = #Original_Code)
AND (TestDate = #Original_TestDate)
AND ((#IsNull_Description = 1 AND Description IS NULL) OR (Description = #Original_Description))
AND (PointsEarned = #Original_PointsEarned)
AND ((#IsNull_PointsAvailable = 1 AND PointsAvailable IS NULL) OR (PointsAvailable =
#Original_PointsAvailable))
AND ((#IsNull_Average25th = 1 AND Average25th IS NULL) OR (Average25th = #Original_Average25th))
AND ((#IsNull_Average75th = 1 AND Average75th IS NULL) OR (Average75th = #Original_Average75th))
AND ((#IsNull_ImportedDate = 1 AND ImportedDate IS NULL) OR (ImportedDate = #Original_ImportedDate))
AND ((#IsNull_CreationDate = 1 AND CreationDate IS NULL) OR (CreationDate = #Original_CreationDate))
AND (TestNum = #Original_TestNum)
AND (CategoryNum = #Original_CategoryNum));
I tried simplifying the update statement by removing the IsNull sections but that did not work.
In SQL null is not equal (=) to anything—not even to another null, so in your query in case if both values are null (old and new one) you need to take that into account and check values with IS NULL.
I'm seeing this pattern repeated several times in the WHERE clause:
#IsNull_Description = 1 AND Description IS NULL
It means that a variable, #IsNull_SomeColumnName, which is presumably set earlier in the code, has a value of 1, and the column that the variable relates to is currently NULL.
The function IsNull(Param1, Param2) is used to substitute the value of the second parameter for the value of the first parameter if the first parameter IS NULL, and the function returns the value of Param2.
In SQL Server, and quite a few other RDBMSs, the IS NULL syntax is used to check if a value is currently NULL. Here, Description IS NULL will return TRUE if, well, Description is null, and FALSE if it is not.

java.sql.SQLException: [SQL0104] Token <END-OF-STATEMENT> was not valid. Valid tokens: ) OR

I am facing SQL exception while running below query in db2 database.
UPDATE TRINITI.T_PARENT_LOTS_API
SET ACTION_CODE = TXN_CODE19_18_FROP4000_TO4000.TXN_CODE19_18_FROP4000_TO4000_TMDV_TXN_CMD,
NEW_FACTORY_NAME = TXN_CODE19_18_FROP4000_TO4000.TXN_CODE19_18_FROP4000_TO4000_TMDV_SHIP_TO_CD,
OLD_OPERATION = '' '',
NEW_OPERATION = '' '',
NEW_TXN_QTY = TXN_CODE19_18_FROP4000_TO4000.TXN_CODE19_18_FROP4000_TO4000_TMDV_TXN_QTY2,
TRANSFER_FACTORY_NAME = TXN_CODE19_18_FROP4000_TO4000.TXN_CODE19_18_FROP4000_TO4000_TMDV_TRANSFER_FACTORY_NAME,
SUBINV_CODE = TXN_CODE19_18_FROP4000_TO4000.TXN_CODE19_18_FROP4000_TO4000_TMDV_SUBINV_CODE,
TRANSFER_SUBINV_CODE = TXN_CODE19_18_FROP4000_TO4000.TXN_CODE19_18_FROP4000_TO4000_TMDV_TRANSFER_SUBINV_CODE,
NEW_BOM_LEVEL = '' DBK '',
NEW_UOM_CODE = TXN_CODE19_18_FROP4000_TO4000.UOM
Where TRINITI.T_PARENT_LOTS_API.ACTION_CODE Is null
And ((TRINITI.T_PARENT_LOTS_API.OLD_STATE_ID = '18' And
TRINITI.T_PARENT_LOTS_API.NEW_OPERATION = '4000' And
TRINITI.T_PARENT_LOTS_API.OLD_OPERATION = '4000' And
TRINITI.T_PARENT_LOTS_API.COMMENTS = 'SHIP')
Or (TRINITI.T_PARENT_LOTS_API.OLD_STATE_ID = '19'
And TRINITI.T_PARENT_LOTS_API.NEW_OPERATION = '4000'
And TRINITI.T_PARENT_LOTS_API.COMMENTS = 'RECV')
)
And TRINITI.T_PARENT_LOTS_API.TRANSACTION_ID = TXN_CODE19_18_FROP4000_TO4000.CONTROL_NO
Not getting where query went wrong.
SQL0104N is a syntax error. It says it is expecting another ")" somewhere. Looking at your statement, count how many "(" you open and how many closing ")" you have. That seems correct. Now, because Db2 complains about end of statement, you need to check if the statement is really passed to Db2 as given above.
Is there an additional character or formatting that indicates "end of statement"?

SQL Server : IF Condition in WHERE clause

I have a bit parameter
#IsInRetry
If it is false I have to set the where condition to
attempts = 0
else if it is true I have to set the where condition to
attempts > 0
How could this be done?
Try this way:
( (#IsInRetry = 0 and attempts = 0) or (#IsInRetry = 1 and attempts > 0) )
Try this:
WHERE (attempts = #IsInRetry or (#IsInRetry = 1 and attempts > 0))
Try this, should work also for negative attempts :-)
#IsInRetry = (attempts > 0)
Greetings.
This:
if #IsInRetry = 0x0
BEGIN
SELECT * FROM dbo.tbl
WHERE attempts = 0
END
ELSE
BEGIN
SELECT * FROM dbo.tbl
WHERE attempts = 1
END

IF ELSE CONDITION IN SQL WHERE CLAUSE

MY QUERY IS
DECLARE #AutoApprove BIT
SET #AutoApprove = (
SELECT AutoApprove
FROM dbo.CommentBox_Setting
WHERE UserModuleID = #myModuleID
AND PortalID = #portalID
AND CultureCode = #cultureCode
)
From this i will get whether 1 OR 0 (TRUE OR FALSE) furthermore i have
SELECT * FROM ComentBox_Comment
WHERE UpperModuleID = #UpperModuleID
AND ModuleID = #myModuleID
AND portalID = #portalID
AND cultureCode = #cultureCode
AND //Here i need to check condition
(IF(#AutoApprove=0){ THEN isapprove=1}else {do not check})
Note here isapprove is table filedName
I know ,i can do this with long query i need short and easy way.
Help me out.
Try something like
AND CASE WHEN #AutoApprove=0 THEN isapprove ELSE 1 END = 1
This will check isapprove = 1 if #AutoApprove = 0, or 1=1(ignore) otherwise.
CASE (Transact-SQL)