Probably am going crazy, but I am trying to assign a field to NULL in a case statement, however, I get an error on the equals next to the NULL keyword
Here is the SQL:
SELECT CASE WHEN enq.IntField = 0 THEN enq.IntField = NULL ELSE enq.IntField END As IntField
FROM [Table]
Surely I am doing something stupid!!
Thanks in advance
Try this
CASE WHEN enq.IntField = 0 THEN NULL ELSE enq.IntField END
Related
In my select statement I calculate some values and then need to calculate a new value based on the results of the already calculated values. How can this be archived without using a temp table?
SELECT
CASE WHEN [Customer] = '173220000' THEN '1' ELSE NULL END AS Ver_a
, CASE WHEN [Service] = '173220000' THEN '1' ELSE NULL END AS Ver_b
, CASE WHEN [Productavailability] = '173220000' THEN '1' ELSE NULL END AS Ver_c
, (SUM(Ver_a, Ver_b, Ver_c)/3) AS Identity_Ver
FROM x
Just use a sub-query/derived table:
SELECT
Ver_a
, Ver_b
, Ver_c
, (coalesce(Ver_a,0) + coalesce(Ver_b,0) + coalesce(Ver_c,0))/3 AS Identity_Ver
FROM (
SELECT
CASE WHEN [Customer] = '173220000' THEN '1' ELSE NULL END AS Ver_a
, CASE WHEN [Service] = '173220000' THEN '1' ELSE NULL END AS Ver_b
, CASE WHEN [Productavailability] = '173220000' THEN '1' ELSE NULL END AS Ver_c
FROM x
) x
Note 1: as you are not grouping you don't need the sum function, just the sum operator (+) - and the sum function doesn't take a comma separated list of values either.
Note 2: your sum won't work without using the coalesce function as you are returning null from the case expressions.
Firstly, you cannot do sum(var1, var2, var3).
The answer to your question, as stated, seems to be the code at the end.
BUT, I have to ask: what do you want to accomplish? Your approach does make sense to me :-(
SELECT
Sum(
CASE WHEN [customer] = '173220000' THEN 1 ELSE 0 END +
CASE WHEN [service] = '173220000' THEN 1 ELSE 0 END +
CASE WHEN [productavailability] = '173220000' THEN 1 ELSE 0 END
) / 3 AS Identity_Ver
FROM x
I recently ran into this pickle with the CASE-THEN-ELSE statement in SQL Server (2014 if it matters), to be more accurate, "the Simple" vs "the Searched" CASE expression. Until now, I thought that the only difference between these 2 is simply the format and/or the habit in writing both ways of the case expression but I guess I was completely wrong :)
MSDN Link
The CASE expression has two formats:
The simple CASE expression
compares an expression to a set of simple expressions to determine the
result.
The searched CASE expression evaluates a set of Boolean
expressions to determine the result.
Here is the example:
set nocount on
declare #test nvarchar(50) = null
select
#test as [The NULL Value],
case
when #test is null
then null
else 'Not Null???'
end as [As Expected],
case #test
when null
then null
else 'Not Null???'
end as [The Pickle]
And the result is:
The NULL Value As Expected The Pickle
-------------------------------------------------- ----------- -----------
NULL NULL Not Null???
Could someone provide a link to a MSDN documentation where this is explained, perhaps in a more detailed manner? :)
P.S.: I bet a lot of you folks were certain that both results would yield the same output :D
It's not weird at all ...
The "shortcut" way of
case #test
when null
then null
else 'Not Null???'
end as [The Pickle]
evaluates the variable/column (here: #test) against the values in the WHEN clauses (when null) with the regular equality operator - and comparing NULL using the standard equality operator (#test = null) is always undefined/NULL itself (standard SQL behavior), so it's not true
Therefore you are getting this result - Not Null??? - for your column The Pickle
If you want to check for NULL, you must use IS NULL like in your first example...
declare #t int =1
--simple case
select
case #t
when 1 then 1 else null end
The above query is expanded to below form..
select
case when #t=1 then 1 else null end
so a query with null will expand to below
declare #t int=null
select case #t
when null then null else 'notnull' end
is expanded to
select case when #t=null then null else 'notnull' end
which obviously evaluates to not null..
So in summary only in null case you will not get results you are expecting,try below to see
declare #t int=null
declare #t1 int =1
select
case when #t is null then null else 'notnull' end as 'Searchedcase',
case #t when null then null else 'notnull' end as'simple case',
case when #t1 =1 then 1 else null end as 'Searchedcase for value',
case #t1 when 1 then 1 else null end as'simple case for value'
See discussion of NULL and UNKNOWN from Transact-SQL reference to get a handle on why '=' doesn't work for NULL.
Simple CASE must be implicitly using '=', rather than IS NULL. So to make IS NULL explicit, use a Searched CASE expression.
Maybe Microsoft will add some functionality to the simple CASE expression to if NULL is encountered, then operator 'IS' is used?
I'm trying to understand how nullif is working. I have an error in this statement
select
case 'Null if equal'
when (MakeFlag = FinishedGoodsFlag) then null
else 1
end
from
Production.Product
I get an error:
Incorrect syntax near '='.
So any help ? Thanks in advance
I guess you want to use NULLIF which returns null if both are equal, or else the first expression:
SELECT [Null if equal] = NULLIF(MakeFlag, FinishedGoodsFlag)
FROM Production.Product
select case
when (MakeFlag = FinishedGoodsFlag) then null
else 1
end as 'Null if equal'
from Production.Product
I think this would help you
Your syntax is not correct, I think this should be:
select case when (MakeFlag = FinishedGoodsFlag) then null
else 1
end as 'Null if equal'
from Production.Product
Not sure where 'NullIf' comes into this.
How do you check for NULL in a CASE statement, when you're using a Scalar Function?
My original query was ... but it fails
SELECT CASE dbo.fnCarerResponse('')
WHEN NULL THEN 'Pass'
ELSE 'Fail'
END
I read the SO question about using IS NULL, like so ...
SELECT CASE dbo.fnCarerResponse('') IS NULL
WHEN NULL THEN 'Pass'
ELSE 'Fail'
END
but this gives the incorrect syntax near the keyword is error
Can you have a Scalar Function in the CASE ?
You are using the wrong style of CASE - you need to use CASE WHEN <expression> THEN not CASE <expression> WHEN <expression> then:
SELECT CASE
WHEN dbo.fnCarerResponse('') IS NULL
THEN 'Pass'
ELSE 'Fail'
END
SELECT CASE
WHEN dbo.fnCarerResponse('') IS NULL
THEN 'Pass'
ELSE 'Fail'
END
SELECT CASE
WHEN dbo.fnCarerResponse('') is NULL THEN 'Pass'
ELSE 'Fail'
END
You can use this way too:
SELECT CASE ISNULL(dbo.fnCarerResponse(''),'NULLVALUE')
WHEN 'NULLVALUE' THEN 'Pass'
ELSE 'Fail'
END
When used in the context of a list, such as the definitions of the columns of a SELECT query, the entire expression must be wrapped in parentheses, like this.
,LastEditorEmail = (SELECT CASE
WHEN CF.LastModifiedBy IS NULL THEN
NULL
ELSE
UE.Email
END)
I currently have a select statement that checks several columns to see if they have data. if any of them are null then i want a bit set to false. if none of them are null then i want a bit set to true. here's what i currently have:
select
cast(
case when ChangeOrderNumber is null then 0 else 1 end *
case when ClientName is null then 0 else 1 end *
case when QuoteNumber is null then 0 else 1 end *
case when ClientNumber is null then 0 else 1 end *
case when ServiceLine is null then 0 else 1 end *
case when ServiceLineCode is null then 0 else 1 end *
case when GroupLeader is null then 0 else 1 end *
case when CreatedBy is null then 0 else 1 end *
case when PTWCompletionDate is null then 0 else 1 end *
case when BudgetedHours is null then 0 else 1 end *
case when BudgetDollars is null then 0 else 1 end *
case when InternalDeadlineDate is null then 0 else 1 end *
case when ProjectDescription is null then 0 else 1 end *
case when Sales is null then 0 else 1 end *
case when Coop is null then 0 else 1 end *
case when PassThrough is null then 0 else 1 end *
case when POStatus is null then 0 else 1 end *
case when PONumber is null then 0 else 1 end as bit
)
as Flag
from t
now, that code works, but it's a bit lengthy, i was wondering if anyone knew of a better way to do this. please note that there are several data types being checked.
further details:
this code is in a view that is being looked at in an application for processing change orders. before a change order can be processed it must meet some data quality checks. this view shows if any of the required data is null.
Just add them up since NULL + "something" is always NULL ...
CREATE TABLE #test(column1 int,column2 varchar(4),column3 float)
INSERT #test VALUES(2,'2',2)
INSERT #test VALUES(0,'1',0)
INSERT #test VALUES(null,'1',0)
INSERT #test VALUES(1,null,0)
INSERT #test VALUES(0,'1',null)
INSERT #test VALUES(null,null,null)
SELECT CASE
WHEN column1 + column2 + column3 is NULL THEN 0 ELSE 1 END, *
FROM #test
from a post I created over 3 years ago ...
Keep in mind that if you have characters that are not numbers that you have to convert to varchar ...
INSERT #test VALUES(0,'abc',null)
Here is the conversion, no need to convert the varchar columns
SELECT CASE WHEN CONVERT(VARCHAR(100),column1)
+ column2
+CONVERT(VARCHAR(100),column3) is NULL THEN 0 ELSE 1 END,*
FROM #test
I think I might go with this solution unless someone comes up with a better one, inspired by #Alireza:
cast(
case when (ChangeOrderNumber is null or
a.ClientName is null or
a.QuoteNumber is null or
ClientNumber is null or
ServiceLine is null or
ServiceLineCode is null or
GroupLeader is null or
CreatedBy is null or
PTWCompletionDate is null or
BudgetedHours is null or
BudgetDollars is null or
InternalDeadlineDate is null or
ProjectDescription is null or
Sales is null or
Coop is null or
PassThrough is null or
POStatus is null or
PONumber is null) then 'false' else 'true'
end as bit) as Flag
Please use IIF() (need to be sql server 2012 or later) I really recommend:
IIF(column1 is null, '0', '1')
What about this one?
select not(a is null or b is null or ...)
You could invert the logic.
SELECT
CASE WHEN ChangeOrderNumber IS NOT NULL
AND ClientName IS NOT NULL
AND QuoteNumber IS NOT NULL
....
THEN 1
ELSE 0
END [Flag]
FROM t
Create a HasValue function that takes in a sql_variant and returns a bit. Then use bitwise AND in your SELECT clause.
CREATE FUNCTION dbo.HasValue(#value sql_variant) RETURNS bit
AS
BEGIN
RETURN (SELECT COUNT(#value))
END
GO
SELECT dbo.HasValue(ChangeOrderNumber)
& dbo.HasValue(ClientName)
& dbo.HasValue(QuoteNumber)
...
as [Flag]
FROM t
Or this:
declare #test1 char(1)
declare #test2 char(1)
declare #outbit bit
set #test1 = NULL
set #test2 = 'some value'
set #outbit = 'True'
select #test1
select #test2
If #test1 + #test2 IS NULL set #outbit = 'False'
Select #outbit
Much simpler -- just use the COALESCE function, which returns the value in the first non-null column.
SELECT Flag = CASE
WHEN COALESCE (column1, column2, column3, ...) IS NULL THEN 0
ELSE 1
END
FROM MyTable