Sybase proc throws "Incorrect syntax near '='" - sql

I have the following piece of a SELECT query inside a stored procedure i'm developing:
AND (
CASE S.SWITCH
WHEN 'A' THEN P.TEST = T.OPTION_1
WHEN 'C' THEN P.TEST = T.OPTION_1 + T.OPTION_2
WHEN 'G' THEN P.TEST = T.OPTION_3
WHEN 'N' THEN TRUE
ELSE FALSE
END
)
I'm getting an Incorrect syntax near '=' error. Why would it complain about the first equals sign? It's a Sybase server if anyone is interested.

You case comparison should be something like below, if you are testing P.TEST value based on S.SWITCH case.
AND (
P.TEST =
CASE
WHEN S.SWITCH = 'A' THEN T.OPTION_1
WHEN S.SWITCH = 'C' THEN T.OPTION_1 + T.OPTION_2
WHEN S.SWITCH = 'G' THEN T.OPTION_3
WHEN S.SWITCH = 'N' THEN TRUE
ELSE FALSE
END
)
If you are comparing based on P.TEST and S.SWITCH, you can do either of following
Blorgbeard already provided this answer
AND
(
(S.SWITCH = 'A' AND P.TEST = T.OPTION_1) OR
(S.SWITCH = 'C' AND T.OPTION_1 + T.OPTION_2) OR
(S.SWITCH = 'G' AND P.TEST = T.OPTION_3) OR
(S.SWITCH = 'N')
)
If you want to make case statement work for this, following could be a possible solution.
AND (
CASE 1 =
WHEN S.SWITCH = 'A' AND P.TEST = T.OPTION_1 THEN 1
WHEN S.SWITCH = 'C' AND P.TEST = T.OPTION_1 + T.OPTION_2 THEN 1
WHEN S.SWITCH = 'G' AND P.TEST = T.OPTION_3 THEN 1
WHEN S.SWITCH = 'N' THEN 1
ELSE 0
END
)

Boolean expressions don't work like that in SQL. You can reformulate your switch like this:
AND (
(S.SWITCH = 'A' AND P.TEST = T.OPTION_1) OR
(S.SWITCH = 'C' AND T.OPTION_1 + T.OPTION_2) OR
(S.SWITCH = 'G' AND P.TEST = T.OPTION_3) OR
(S.SWITCH = 'N')
)

I know this is more of a comment than an answer, but hopefully this will lead to an answer, and I need to do formatted code for this so...
I tried this in Mysql and it worked. Could you try something like this in Sybase and see what it returns? The point being, extract the part that failed and test it out and see if you can figure out exactly what is wrong. It may be that Sybase is pointing to that equal sign but something else is really what is confusing it.
set #miller:='C';
set #mtime:=2;
select CASE #miller
WHEN 'A' THEN #mtime = 1
WHEN 'C' THEN #mtime = 2
WHEN 'G' THEN #mtime = 3
WHEN 'N' THEN TRUE
ELSE FALSE
END
This returns a 1 since miller = 'C' takes it to check if mtime = 2, which is true, and that means a 1 or true in Mysql.
Could you try and isolate this bit of code like this in Sybase?

Related

Concatenation by using CASE statement

I want to CONCAT between this on my SELECT statement in SQL Server.
All the columns are booleans.
The output will be like this if all of columns that are true
GGPNES
I tried to use this and it doesn't work
DECLARE #concat VARCHAR(40) ='';
SELECT
Smoke, Invoice,
Party, Summary,
MySelf, Export,
CASE
WHEN MyTable.Smoke = 1 OR MyTable.Invoice = 1
THEN #concat + 'GG'
WHEN MyTable.Party = 1
THEN #concat + 'P'
WHEN MyTable.Summary = 1
THEN #concat + 'N'
WHEN MyTable.MySelf = 1
THEN #concat + 'E'
WHEN MyTable.Export = 1
THEN #concat + 'S'
END
FROM
MyTable
I think that you want a concatenation of CASE expressions:
CASE WHEN Smoke = 1 OR Invoice = 1 THEN 'GG' ELSE '' END +
CASE WHEN Party = 1 THEN 'P' ELSE '' END +
CASE WHEN Summary = 1 THEN 'N' ELSE '' END +
CASE WHEN MySelf = 1 THEN 'E' ELSE '' END +
CASE WHEN Export = 1 THEN 'S' ELSE '' END

How to correctly place brackets in a CASE statement?

Case Statement:
(CASE WHEN v_cur.order_type != 'STK'
AND v_cur.lot_net_cost_flag = 'Y' THEN 0
ELSE ( (v_cur.unit_costs_amount * v_cur.line_quantity_ordered)
/ CASE WHEN v_cur.unit_of_measure_code = 'E' THEN 1
WHEN v_cur.unit_of_measure_code = 'C' THEN 100
WHEN v_cur.unit_of_measure_code = 'M' THEN 1000
ELSE 1 END ) v_dt_unit_of_measure_code_val )
END v_dt_cost_amount
case
when v_cur.order_type != 'STK' and v_cur.lot_net_cost_flag = 'Y' then 0
else v_cur.unit_costs_amount * v_cur.line_quantity_ordered /
case v_cur.unit_of_measure_code
when 'E' then 1
when 'C' then 100
when 'M' then 1000
else 1
end
end as v_dt_cost_amount
CASE syntax does not require any brackets. You only need them for mixed OR/AND logical conditions and for non-default arithmetic precedence, which you don't have in the above expression because (x * y) / z is the same thing as x * y / z.
delete v_dt_unit_of_measure_code_val
CASE WHEN v_cur.order_type != 'STK'
AND v_cur.lot_net_cost_flag = 'Y' THEN 0
ELSE ( (v_cur.unit_costs_amount * v_cur.line_quantity_ordered)
/ CASE WHEN v_cur.unit_of_measure_code = 'E' THEN 1
WHEN v_cur.unit_of_measure_code = 'C' THEN 100
WHEN v_cur.unit_of_measure_code = 'M' THEN 1000
ELSE 1 END )
END v_dt_cost_amount

How to use bitwise operator in existing sql query?

Here is my sql query. I have column name "ExpenseBucketCoverage" in claim table in which I am storing bitwise operators store multiple values in one column like below
MED_COPAY = 1, MED_DED= 10, MED_COINS = 100, RX_COPAY = 1, RX_DED= 10, RX_COINS = 100
I want to replace hard coded value like MED_COPAY, MED_COINS, MED_DED, RX_DED, RX_COINS & RX_COPAY in query by using ExpenseBucketCoverage column value. Can some one please tell me how can I do that?
Someone has suggested me below soultion
retrieve data from claim and left joining the first matched record in eligibility. And then add custom code to loop through the datarows to split the rows by covered expense bucket, and set the service category code in-memory column based on the ExpenseBucketCoverage value for the claim.
SELECT
e.categoryid,
c.servicetype,
'II' AS RepordType,
e.TPAId AS TPA_Id,
e.EmployerCode,
e.SubscriberId,
e.MemberID,
c.ServiceFrom,
c.ServiceTo,
CASE
WHEN e.categoryid IN( 'MED_DED', 'RX_DED' ) THEN
deductible
WHEN e.categoryid IN( 'MED_COINS', 'RX_COINS' ) THEN
isnull(coins,0)
WHEN e.categoryid IN( 'MED_COPAY', 'RX_COPAY' ) THEN
copay
ELSE 0
END AS ClaimAmount,
'' AS AccountTypeCode,
'1' ClaimsCrossoverAutoPay,
e.CategoryId,
CASE c.ServiceType
WHEN 'H' THEN
CASE e.PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS PayProvider,
CASE c.ServiceType
WHEN 'H' THEN
CASE PayeeIndicator
WHEN 'N' THEN '0'
WHEN 'Y' THEN '1'
END
WHEN 'P' THEN '0'
END AS ReimbursementMethod,
CASE c.ServiceType
WHEN 'H' THEN c.Provider
WHEN 'P' THEN ''
END AS ProviderId,
'1' EnforceAccountEffectiveDates,
c.Id,
c.ClaimNumber + e.CategoryId as 'ExternalClaimNumber',
c.ProviderName,
c.CarrierId + ';' + c.SourceClaimNumber AS Notes
FROM Claim c
INNER JOIN Eligibility e ON e.TPAId = c.TPAId AND e.EIN = c.EIN AND
c.Processed = 'Y' AND e.FilterType = 'Eligibility'
AND c.TPAId='PrimePay'
AND (c.ServiceFrom >= e.BenefitEffectiveDate
AND c.ServiceFrom <=e.BenefitTermDate)
AND ( ( c.PayorID = c.PatientSSN
AND e.SubscriberSSN = c.PatientSSN
AND (c.EmployeeFirstName = c.PatientFirstName
AND c.EmployeeLastName = c.PatientLastName)
AND(e.MemberSSN = '' OR e.MemberSSN = NULL)
AND(e.MemberFirstName = '' OR e.MemberFirstName = NULL)
AND(e.MemberLastName = '' OR e.MemberLastName = NULL))
OR((c.PayorID != c.PatientSSN AND e.MemberSSN = c.PatientSSN
AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName = c.PatientLastName)
OR(c.PayorID != c.PatientSSN AND e.MemberFirstName = c.PatientFirstName
AND e.MemberLastName= c.PatientLastName)))
AND (( c.Servicetype ='P'
AND e.CategoryID IN('RX_COINS','RX_COPAY', 'RX_DED' ))
OR ( c.Servicetype = 'H'
AND e.CategoryID IN( 'MED_COINS','MED_COPAY', 'MED_DED' )))

Convert case statement to logical operator in the where clause

Can you please help me convert this piece of code to logical operator and use 'like' not '=' cause i know it's not efficient to put case statement on the where clause. Tia! Here's the code.
Where
(#reftype = '0' or
(#refnum = case #reftype when 'BN' then refBN when 'C' then refC when 'O' then refO else 'n/a' end)
)
Replace
or #refnum = case #reftype when 'BN' then refBN when 'C' then refC when 'O' then refO else 'n/a' end)
with
or (#reftype = 'BN' AND #refnum = refBN)
or (#reftype = 'C' AND #refnum = refC)
or (#reftype = 'O' AND #refnum = refO)
or #refnum = 'n/a'
You can translate it to boolean logic in this way:
WHERE #reftype = '0 'OR #refnum = 'n/a'
OR (#reftype = 'BN' AND #refnum = refbn)
OR (#reftype = 'C' AND #refnum = refc)
OR (#reftype = 'O' AND #refnum = refo)
But note that CASE in the WHERE-clause is not inefficient per se.

Concatenating multiple CASE statements into one alias

After some previous help on how to approach a problem I am having with some legacy code, it seems like the best approach for my issue is to concatenate case statements to return a value I can parse out in PHP.
I am trying to do something like this, but it is returning many rows, and eventually getting this error:
Maximum stored procedure, function, trigger, or view nesting level
exceeded (limit 32).
SELECT org.org_id,
org.org_name_1,
Datename(YEAR, member.enroll_date) AS enroll_year,
Max(CASE
WHEN board.member_from IS NULL THEN 0
ELSE 1
END) AS board_member,
CASE
WHEN ( org.delete_reason = 'OUT'
AND org.org_delete_flag = 'Y'
AND org.org_status_flag = 'C' ) THEN 'out_of_business|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues > 0 ) THEN 'insurance_member|'
ELSE ''
END + CASE
WHEN ( stat.carrier = 'BS'
AND stat.status_id IS NOT NULL
AND stat.termination_date IS NULL
AND stat.flat_dues = 0
AND member.status_flag IN( 'C', 'P' ) ) THEN 'insurance_product|'
ELSE ''
END + CASE
WHEN ( member.enroll_date IS NOT NULL
AND member.status_flag NOT IN( 'C', 'P' ) ) THEN 'member_since|'
ELSE ''
END + CASE
WHEN ( org.org_relationship_parent = 'Y'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 'subsidiary_member|'
ELSE ''
END + CASE
WHEN ( org.org_misc_data_9 = 'PAC' ) THEN 'pac|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'PART' ) THEN 'partner_member|'
ELSE ''
END + CASE
WHEN ( org.dues_category = 'FREE'
AND org.org_status_flag = 'P' ) THEN 'associate_member|'
ELSE ''
END
--ELSE 'non_member'
--END
AS org_status,
60 AS expires_in,
CASE
WHEN stat.dues_type = 'M' THEN
CASE
WHEN ( stat.termination_date IS NULL ) THEN ( stat.flat_dues )
ELSE 0
END
ELSE
CASE
WHEN ( member.payments = 0 ) THEN member.dues_billed_annual
ELSE member.payments
END
END AS dues_level,
CASE
WHEN ( org.affiliate_code = 'PCCE'
AND org.dues_category = 'MBR'
AND org.org_status_flag = 'R' ) THEN 1
ELSE 0
END AS pcce_membr,
-- '$'+CONVERT(VARCHAR,#dues) AS dues_level,
Ltrim(#product_level) AS product_level,
Ltrim(#involve_level) AS involvement_level
FROM organiz AS org
LEFT JOIN affilbil AS member
ON member.status_id = org.org_id
AND member.dues_category = 'MBR'
LEFT JOIN individu AS ind
ON ind.org_id = org.org_id
LEFT JOIN commembr AS board
ON board.status_id = ind.ind_id
AND board.committee_code = '5'
AND board.member_to IS NULL
LEFT JOIN statinsmorn AS stat
ON stat.status_id = org.org_id
AND stat.carrier = 'BS'
AND stat.planz = 'PCI'
WHERE org.org_id = #org_id
GROUP BY org.org_id,
org.org_name_1,
member.enroll_date,
org.delete_reason,
org.org_status_flag,
org.org_delete_flag,
stat.status_id,
stat.flat_dues,
stat.dues_type,
stat.termination_date,
org.org_misc_data_9,
org_relationship_parent,
org.dues_category,
member.status_flag,
member.dues_billed_annual,
member.payments,
stat.carrier,
org.Affiliate_Code
Well, this is embarrassing.
When I was making my changes to the stored procedure, I had inadvertently placed a call to the same procedure at the bottom. So I was recursively calling the same procedure over and over again. DOH.