SELECT IF into custom variable - sql-server

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.

Related

Convert Access SQL IIF to T-SQL CASE statement

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

SQL Server 2012 and NULL comparison

Can anyone explain me why these two statements returns different results?
SELECT CASE WHEN NOT((NULL = NULL) OR (1 != 1)) THEN 1 ELSE 0 END
SELECT CASE WHEN NOT((NULL = NULL) AND (1 != 1)) THEN 1 ELSE 0 END
I know that NULL compared with anything gives false and I wanted to use that property but I stopped at commands similar to above. My real statements instead of NULLs use variables that can be NULL but I simplified them to show where is the problem. I thought that it has something with operation order but it seems that's not it.
I know that NULL compared with anything gives false
This isn't correct, NULL compared with anything evaluates to unknown, not false, a quick example:
SELECT CASE WHEN (NULL = NULL) THEN 'True'
WHEN NOT(NULL = NULL) THEN 'False'
ELSE 'Other'
END
Will give the third option of Other.
If we rewrite your logic (still the same meaning, but it becomes more clear):
SELECT CASE WHEN (NULL <> NULL) AND (1 = 1) THEN 1 ELSE 0 END
SELECT CASE WHEN (NULL <> NULL) OR (1 = 1) THEN 1 ELSE 0 END
So in the first instance you have WHEN [Unknown] AND [True] which is false, but in the second you have WHEN [Unknown] OR [True] which is true, so returns 1.
If you rewrite the query with variables, then inspect the execution plan XML, you can see that SQL Server rewrites the expression as above during compilation:
DECLARE #a INT = NULL, #b INT = NULL, #c INT = 1, #d INT = 1;
SELECT TOP 1
CASE WHEN NOT((#a = #b) OR (#c != #d)) THEN 1 ELSE 0 END,
CASE WHEN NOT((#a = #b) AND (#c != #d)) THEN 1 ELSE 0 END
-- first query
SELECT CASE WHEN NOT((NULL = NULL) AND (1 != 1)) THEN 1 ELSE 0 END
=
SELECT CASE WHEN NOT(unknown AND false) THEN 1 ELSE 0 END
=
SELECT CASE WHEN NOT(false) THEN 1 ELSE 0 END
=
SELECT CASE WHEN true THEN 1 ELSE 0 END
=
1
-- second query
SELECT CASE WHEN NOT((NULL = NULL) OR (1 != 1)) THEN 1 ELSE 0 END
=
SELECT CASE WHEN NOT(unknown OR false) THEN 1 ELSE 0 END
=
SELECT CASE WHEN NOT(unknown) THEN 1 ELSE 0 END
=
SELECT CASE WHEN unknown THEN 1 ELSE 0 END
=
else matched, so 0
And to D0dger's question from comments:
It's more interesting why SELECT CASE WHEN (NULL = NULL) OR (1 != 1) THEN 1 ELSE 0 END and SELECT CASE WHEN NOT((NULL = NULL) OR (1 != 1)) THEN 1 ELSE 0 END returns 0
SELECT CASE WHEN (NULL = NULL) OR (1 != 1) THEN 1 ELSE 0 END
=
SELECT CASE WHEN unknown OR false THEN 1 ELSE 0 END
=
SELECT CASE WHEN unknown THEN 1 ELSE 0 END
=
else matched, so 0
OR (Transact-SQL), AND (Transact-SQL)
So, there are 2 options: Either you have ANSI_NULLS ON (and you should) or you have ANSI_NULLS OFF.
In the first case, any comparison with NULL returns NULL (even comparisons between NULL values such as yours).
In the second case, sql server will evaluate comparisons between NULL values (e.g. NULL=NULL will return true).
So before considering the different results in your queries you must first consider that comparing NULL with anything, doesn't evaluate to false but NULL

How do you write an IF ELSE inside a SELECT statement in MSSQL 2008?

Here is what I am trying to do:
SELECT iif(d.ItemType = 'INVOICE', 0, iif(d.ItemType = 'PACKING', 0, 1)) AS MissingDocuments FROM dbo.DocumentDetails AS D
Unfortunately realized this is not compatible with MSSQL2008. So tried writing an IF ELSE but that also is not working.
SELECT IF d.ItemType = 'INVOICE'
0
ELSE
BEGIN
d.ItemType = 'PACKING'
0
ELSE
1
END AS MissingDocuments
FROM dbo.DocumentDetails AS D
can you please tell me what am I doing wrong here ?
Use CASE ... WHEN. The most concise logic seems to be:
SELECT
CASE WHEN d.ItemType IN ('INVOICE', 'PACKING') THEN 0 ELSE 1 END
AS MissingDocuments
FROM dbo.DocumentDetails AS d
i.e. the Document is missing if it isn't 'Invoice' or 'Packing'
I think You Looking For Case When
Try This..
SELECT
case when d.ItemType = 'INVOICE' or d.ItemType = 'PACKING'
then 0
ELSE
1
END AS MissingDocuments
FROM dbo.DocumentDetails AS D

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).

'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

Resources