NullIf statement in SQL Server - sql-server

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.

Related

Error with SELECT CASE with regular expression

I wrote the following query in T-SQL for SQL Server
SELECT
CASE
WHEN ADDR_LINE_1 REGEXP '^[0-9]'
THEN SUBSTRING(ADDR_LINE_1,1,CHARINDEX(' ',ADDR_LINE_1))
ELSE NULL
END AS HOUSE_NUMBER
FROM CUSTOMER
What I want is that if the column ADDR_LINE_1 starts with a number, I want to extract the HOUSE_NUMBER from it. But right now my query gives a parse error. If I replace the word REGEXP with LIKE, the parse error goes away, but I always get NULL for HOUSE_NUMBER. What is the correct syntax for my query?
You could use ISNUMERIC and LEFT like this.
SELECT
CASE WHEN ISNUMERIC(LEFT(ADDR_LINE_1, 1)) = 1
THEN SUBSTRING(ADDR_LINE_1, 1, CHARINDEX(' ', ADDR_LINE_1))
ELSE NULL
END AS HOUSE_NUMBER
FROM CUSTOMER
How about using the LIKE
SELECT
CASE
WHEN ADDR_LINE_1 Like '%[0-9]%'
THEN SUBSTRING(ADDR_LINE_1,1,CHARINDEX(' ',ADDR_LINE_1))
ELSE NULL
END AS HOUSE_NUMBER
FROM CUSTOMER

T-SQL - Assign field to NULL in a case statement

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

SQL Server 2008 Case When Cast

I'm having trouble figuring this statement out. It seems that SQL Server is still executing the THEN part in the CASE WHEN statement. Please see this query.
SELECT
CASE
WHEN ISNUMERIC('INC') = 1
THEN CAST('INC' as numeric(10,2))
ELSE 'FALSE'
END AS foo
SQL Server is returning
"Error converting data type varchar to numeric"
From this query it should return FALSE and not return an Error since the THEN part was not executed.
What is wrong with my query?
The problem is that you are returning two different data types from the same column. So try this one -
DECLARE #value CHAR(3)
SET #value = 66
SELECT
CASE WHEN ISNUMERIC(#value) = 1
THEN CAST(CAST(#value AS NUMERIC(10,2)) AS VARCHAR(30))
ELSE 'FALSE'
END AS foo
That is because, your query is trying to
CAST 'FALSE' as Numeric(10,2)
Try this
SELECT CASE WHEN ISNUMERIC('INC') = 1 THEN
CAST(CAST('INC' as numeric(10,2)) AS varchar(5))
ELSE 'FALSE' END AS foo
The problem is that one branch of you CASE branches returns VARCHAR and the other a number.
Try the following:
CASE WHEN ISNUMERIC('INC') = 1
THEN CAST(CAST('INC' AS NUMERIC(10,2)) AS NVARCHAR)
ELSE 'FALSE'
END AS foo

How to check for NULL in a CASE statement with a Scalar Function?

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)

SELECT 1=1 not working

My question is a bit more generic, but let's say I want to test some expression in SQL Server. I write SELECT 1=1 or SELECT 'a' > 'B' and press F5, hoping to see the result, like I do when I type SELECT 0, 1.
But I get an error instead. Why is that? What should I use to evaluate those expressions on the fly?
SQL Server doesn't have a boolean data type.
You would need to use SELECT CASE WHEN 1=1 THEN 1 ELSE 0 END
Simplest way is to select 1 where <test expression here>
You could put your expression after where like this
select 'true' where 1=1
select 'true' where 1<>1
of you could put it in an IF statement
IF 1+1=2
BEGIN
PRINT 'One and one makes two.'
END
You could use a case statement:
select case when 1=1 then 'true' else 'false' end
select case when 'a'>'B' then 'true' else 'false' end
or an IF...ELSE
if 1=1
select 'true'
else
select 'false'
if 'a' > 'B'
select 'true'
else
select 'false'
You can use the following to turn the output of the comparison to pseudo-boolean:
select isnull((select 'false' where not(1**0=1**)) ,'true')
or, to have an int as return:
select isnull((select 1 where 1<1) ,0)

Resources