Convert Access SQL IIF to T-SQL CASE statement - sql-server

I have someone else's Access query that I need to convert to T-SQL. I have it mostly done. These next two lines are my issue. This is the Access SQL:
...
, [MATERIALVALUE]/1000 AS COSTOURS
...
, IIf([ALLJOBS].[SALESID]="SUI",[COSTOURS]=0,IIf([PRICE]=-1,[COSTOURS]=0,[COSTOURS])) AS COST0
...
The first line COSTOURS is simple enough. It is just
...
,MATERIALVALUES/1000 COSTOURS
...
What I am having problems with is the setting of COSTOURS later in the same SELECT statement's CASE statement as well as what is COST0 getting set to?
My T-SQL case statement is this:
,CASE AllJobs.SALESID
WHEN 'SUI' THEN COSTOURS = 0
ELSE CASE ESSDBtRels.PRICE
WHEN -1 THEN COSTOURS = 0
ELSE ESSDBtRels.MATERIALVALUE/1000
END
END COST0
Which doesn't work obviously. How do I set a previously set column with new results based on a subsequent CASE statement's true attributes. And what does COST0 get set to in the case of a TRUE and a FALSE, TRUE occasion? A FALSE, FALSE will set COST0 to COSTOURS.
Thanks #forpas, since MATERIALVALUE has no bearing I will go with the following SQL:
CASE AllJobs.SALESID
WHEN 'SUI' THEN 0
ELSE CASE ESSDBtRels.PRICE
WHEN 1 THEN 0
ELSE ESSDBtRels.MATERIALVALUE/1000
END
END COST0

In MS Access a Boolean expression like [COSTOURS]=0 is evaluated as -1 for TRUE or 0 for FALSE.
So to get the same results you should use a CASE statement in the place of these expressions:
CASE AllJobs.SALESID
WHEN 'SUI' THEN CASE ESSDBtRels.[MATERIALVALUE] WHEN 0 THEN -1 ELSE 0 END
ELSE CASE ESSDBtRels.PRICE
WHEN -1 THEN CASE ESSDBtRels.[MATERIALVALUE] WHEN 0 THEN -1 ELSE 0 END
ELSE ESSDBtRels.MATERIALVALUE/1000
END
END COST0
or with IIF():
IIF(
[ALLJOBS].[SALESID]='SUI',
IIF(ESSDBtRels.MATERIALVALUE=0, -1, 0),
IIf(
[PRICE]=-1,
IIF(ESSDBtRels.MATERIALVALUE=0, -1, 0),
ESSDBtRels.MATERIALVALUE/1000
)
) AS COST0

Related

Select Records Expert equivalent for SSRS

Crystal Reports has the Select Records Expert which allows you to use variables to build WHERE conditions. Is there something similar to the select records experts in SSRS?
If the variable #Restriction below is equal to one, the column name CFE_EDI.ETAT = 'ENV' or CFE_EDI.ETAT = 'OUV'. I tried to mimic the functionality with a SQL query but due to AND and OR precedence, the WHERE condition is not functioning properly.
You can handle this within the SQL of your SSRS Dataset where clause buy using case statements that return a 1 or a 0 depending on whether or not the criteria is met:
where case when #Restriction = '1'
then case when CFE_EDI.ETAT in('ENV','OUV') then 1 else 0 end
when #Restriction = '0'
then case when CFE_EDI.ETAT not in('ENV','OUV') then 1 else 0 end
else case when CFE_EDI.ETAT <> '' then 1 else 0 end
end = 1

IIF to Case statement

I have the below code in Access which I'm migrating to SQL Server,
IIf([code] Is Not Null,IIf([code]<>'ABC',
IIf([length] Is Null,1,IIf([Length]=0,1,
IIf([width] Is Null,1,IIf([Width]=0,1,
IIf([height] Is Null,1,IIf([Height]=0,1,
0))))))))
I believe it checks for code not null and not equal to 'ABC' then length should be null or 0 then it assigns a value 1 of the condition fails then it is 0. I need a little help with writing it with case.
Literal translating:
CASE WHEN [code] Is Not Null
THEN
CASE WHEN [code]<>'ABC'
THEN
CASE WHEN [length] Is Null
THEN 1
ELSE
CASE WHEN [Length]=0
THEN 1
ELSE
CASE WHEN [width] Is Null
THEN 1
ELSE
CASE WHEN [Width]=0
THEN 1
ELSE
CASE WHEN [height] Is Null
THEN 1
ELSE
CASE WHEN [Height]=0
THEN 1
ELSE 0
END
END
END
END
END
END
END
END
Simplified in :
CASE WHEN ISNULL([code], 'ABC') <>'ABC' AND
(ISNULL([length], 0) = 0 OR
ISNULL([width], 0) = 0 OR
ISNULL([height], 0) = 0)
THEN 1
ELSE 0
END
IIf in JET SQL translates to CASE WHEN in SQL Server as follows:
IIf(condition, whenTrue, whenFalse)
translates directly to
CASE WHEN condition THEN whenTrue ELSE whenFalse END
For nested expressions
IIf(condition1, whenTrue1, IIf(condition2, whenTrue2, whenFalse))
you can either translate them directly:
CASE WHEN condition1
THEN whenTrue1
ELSE CASE WHEN condition2
THEN whenTrue2
ELSE whenFalse
END
END
or use the fact that CASE allows you to specify multiple WHEN conditions:
CASE WHEN condition1 THEN whenTrue1
WHEN condition2 THEN whenTrue2
ELSE whenFalse
END
With this knowledge, translating your statement should be easy and is left as an exercise to the reader (I don't want to spoil the fun).

SQL Computed Column If Statement

This is not working. Still getting division by zero error. Any ideas?
case when [games]=NULL then (0)
when [games]=(0) then (0)
else CONVERT([decimal](18,2),CONVERT([float],[goalsAG],(0))/CONVERT([float],[games],(0)),(0)) end
You can never use = with NULL, use IS NULL.
And simplified further:
(case when isnull([games], 0) = 0 then (0) else CONVERT(decimal,CONVERT([float],[goalsAG],(0))/CONVERT([float],[games],(0)),(0)) end)

'an expression of non boolean type specified' in CASE WHEN

Given this T-SQL code:
SELECT CASE
WHEN ( Cast (CASE
WHEN Isnull(bpe.new, 0) = 0
AND Isnull(bpe.regular, 0) = 0
AND Isnull(bpe.bargain, 0) = 0 THEN 0
ELSE 1
END AS BIT)
AND #siteID IS NULL
OR bpe.siteid = #siteID ) THEN 1
ELSE 0
END AS SiteHasBrandException
FROM brandpromoexceptions AS bpe
I get the error message:
an expression of non boolean type specified in the context where a condition is expected TSQL
Why is that?
If you mean zero as "false" for that first bit, the query should be equivalent to:
SELECT CASE WHEN
NOT ( ISNULL(bpe.new, 0) = 0
AND Isnull(bpe.regular, 0) = 0
AND Isnull(bpe.bargain, 0) = 0 )
AND ( #siteID IS NULL
OR bpe.siteid = #siteID )
THEN 1
ELSE 0
END AS SiteHasBrandException
FROM brandpromoexceptions AS bpe
You may need to be careful with that "OR" clause, or add parentheses for clarification. I guessed here.
You are passing your first CASE statement a call to CAST rather than a conditional. The error message pretty much spells it out.
I'm not sure exactly what you are trying to do, but you seem to understand that you need to pass a boolean to the CASE WHEN based on your subsequent use of CASE so hopefully this helps.
this is a shot in the dark by try changing your code to this:
SELECT CASE
WHEN ( Cast (CASE
WHEN Isnull(bpe.new, 0) = 0
AND Isnull(bpe.regular, 0) = 0
AND Isnull(bpe.bargain, 0) = 0 THEN 0
ELSE 1
END AS BIT) = 1
AND #siteID IS NULL
OR bpe.siteid = #siteID ) THEN 1
ELSE 0
END AS SiteHasBrandException
FROM brandpromoexceptions AS bpe

SELECT IF into custom variable

I have this:
SELECT
#is_daily_rollup = CASE WHEN rt.[id]=1 THEN 1 ELSE 0,
#is_weekly_rollup = CASE WHEN rt.[id]=2 THEN 1 ELSE 0
But sql server is complaining about syntax. How would I go about implementing this conditional value into a variable?
For a CASE statement you need to provide an END
CASE WHEN rt.[id]=1 THEN 1 ELSE 0 END
CASE WHEN rt.[id]=2 THEN 1 ELSE 0 END
so your full query would be:
SELECT #is_daily_rollup = CASE WHEN rt.[id]=1 THEN 1 ELSE 0 END,
#is_weekly_rollup = CASE WHEN rt.[id]=2 THEN 1 ELSE 0 END
You are missing the END for your CASE:
SELECT
#is_daily_rollup = CASE WHEN rt.[id]=1 THEN 1 ELSE 0 END,
#is_weekly_rollup = CASE WHEN rt.[id]=2 THEN 1 ELSE 0 END
Of course, this is assuming that you already declared yor variables.

Resources