Handle Null values without Case - sql-server

Hi I had a Table called Details which contains column Day1 with datatype as Int.
Now if the Column Day1 contains any number then I want to display 100 else if it contains null I want to display null only.
Apart from writing below case statement is there any more simpler way to do it?:
Case when Day1=10 /*(Apart from 0 it can have any value including null)*/ then 100 else Day1
I had around 31 columns I dont want to write case for each and every column. Is there a better way to do this?

Certainly No, if you are thinking of using ISNULL() or COALESCE() function but you can modify your CASE expression to be like below
Case when Day1 > 0 or Day1 IS NULL then 100 else Day1

Hi you can check for NULL in the CASE-Statement:
SELECT CASE WHEN Day1 IS NULL THEN Day1 ELSE 100 END AS Day1 FROM Details

Well, I didn't exactly get what's your intention of writing such a query, but nevertheless, this case will do it.
CASE WHEN Day1 IS NULL THEN NULL ELSE 100 END

Since you say you want to check whether it is a number or not, ISNUMERIC comes to mind:
CASE WHEN ISNUMERIC(Day1) = 1 THEN 100 ELSE NULL END
, but I think simply writing
CASE WHEN Day1 IS NULL THEN NULL ELSE 100 END
or
IIF(ISNUMERIC(Day1) = 1, 100, NULL)
is more intuitive (see how you don't need a CASE in this case? :-))

If you are using SQL Server 2012 or newer:
IIF(Day1 IS NULL, NULL, 100)
or you can make a user-defined function to do the same thing even more concisely (works with all versions of SQL Server):
CREATE FUNCTION dbo.MyFunc(#Day int)
RETURNS int
RETURN (CASE WHEN #Day IS NOT NULL THEN 100 ELSE NULL END)
Usage:
SELECT MyFunc(Day1), MyFunc(Day2)

Related

How to make SELECT in this case? [duplicate]

This question already has answers here:
Simplifying a query with a nested query
(3 answers)
Closed 1 year ago.
How to create a single MSSQL SELECT query in this case:
We've got 2 tables: fruits and expiration
The goal is to receive table where specific fruit number has information about having NULL in expirationDate column. Those fruit numbers that don't have NULL would have zeros in that column. Number 4 doesn't exist in expiration table, so it would also have 0 in results, because it doesn't have NULL.
Tables
You can't easily get the expiration date on the format you want. However, it does not really matter(?). I assume you want 1 or 0 in your new table because you want to use an if statement to check if the fruit is bad or not.
You can solve this easily:
if(expirationDate == null){/*something*/}
or, you might even be able to do
if(expirationDate) //this is all fruit that is not bad
else{/*your code to deal with expirated fruit here*/}
Note: I don't know what programming languages you are using. But in most of them: null and 0 are FALSE.
if(null) // false
if(0) //false
if(undefined) //false, in javascript
//everything that is not a false value, is true.
if("oiaehgtaoiwgneawg") //true
if(-1) //true
To answer your SQL query question:
You already have everything you need in the expiration table
SELECT fruit_number, expiration_date
FROM expiration;
I hope this helps 😊
You have to use it with Case Condition. But have to change statement a little:
select fruit_number, --distinct(fruit_number),
x =case expiration_date
when NULL then null
else 0
end
My friend found the solution.
SELECT
fruit_number,
MAX(expirationDate) as expirationDate
FROM
(SELECT
f.fruit_number,
CASE
WHEN e.expiration_date is NULL AND e.fruit_number IS NOT NULL THEN 1
ELSE 0
END AS expirationDate
FROM
expiration as e
FULL OUTER JOIN fruits as f ON f.fruit_number = e.fruit_number
WHERE
f.fruit_number IS NOT NULL
) t
GROUP BY
fruit_number
ORDER BY
fruit_number

SQL Server Case Statement when IS NULL - Other SO questions do not solve this issue

There are 2 questions on SO, but they are different and do not solve this problem. Feel free to test it before marking this as duplicate.
There is a SQLFiddle for this question.
In this example, the cell phone number may be NULL
ID
Name
Cell
1
John
123
2
Sally
NULL
The query works when the cell number is not null:
DECLARE #Cell NVARCHAR(100) = '123'
SELECT *
FROM Temp
WHERE Cell = CASE WHEN #Cell IS NULL THEN NULL ELSE #Cell END
The same query fails when the cell number is null.
DECLARE #Cell NVARCHAR(100) = NULL
SELECT *
FROM Temp
WHERE Cell = CASE WHEN #Cell IS NULL THEN NULL ELSE #Cell END
The question is how to get the CASE WHEN working for both NULL and when it is comparing an actual value. Note that this is a simplified example of the real problem (which has a lot more conditions and additional complexity) and the focus is to get the example working by modifying the CASE WHEN in order to solve the real problem.
NULL isn’t equal to anything, including NULL but you can just check if something is NULL
WHERE (#Cell IS NULL AND Cell IS NULL) OR Cell = #Cell
Probably could also move the comparison inside CASE but this is clear in meaning at least.
You can do it without the CASE expression, with COALESCE():
DECLARE #Cell NVARCHAR(100) = ?
Select * from Temp
WHERE Cell = #Cell OR COALESCE(Cell, #Cell) IS NULL;
Replace ? with the value that you search for or NULL.
See the demo.

Is <> 0 condition will Consider Null & 0 as same in SQL

I need to compare variable in case condition in stored procedure.
case when #column <> 0 then ...
ELSE 0 END
but whenever am getting #column as NULL, then the above script returning as 0.
will sql consider both 0 & NULL as same.
Am using sql server 2014.
Thanks All
No. SQL considers NULL as "I have no idea". Comparing anything with "I have no idea" results in an answer of "I totally have no idea". Look:
- How high is John?
- I have no idea.
- What is two centimeters higher than John?
- I have no idea.
Even comparison between two NULL values is not true: if I have no idea how tall John is and if I also have no idea how tall Jack is, I can't conclude that John is equally tall as Jack (and I can't conclude that John is not equally tall as Jack). The only sensible answer is... "I have no idea".
The way to test for NULL is with IS operator, which specifically exists for this scenario (e.g. #column IS NULL, or #column IS NOT NULL).
So NULL is not equal to 0, nor is it NOT equal to 0. The result of NULL <> 0 is NULL. However, NULL is falsy where conditionals are concerned, so CASE thinks you should get the ELSE branch any time #column is NULL.
In case if you want to execute the then part of case if the column value is null, then modify your condition to check for nulls also
CASE WHEN (#column <> 0 OR #column IS NULL) then ...
ELSE 0 END

Convert Decode from Oracle to Case from MS SQL Server

I have this line I am struggling with to convert a query from Oracle to SQL Server 2012. the following line is:
DECODE(SUM(DECODE(a.canceldate, NULL, 1,0)), 1, NULL, To_Date(MAX(TO_CHAR(a.canceldate,'yyyymmdd')), 'yyyymmdd')) dCancelDate,
As I inteprete is to convert it like:
case a.canceldate
(when sum(case a.canceldate when Null then 1 else 0 end))
when 1
then 0
else convert(datetime,a.canceldate)
end max(a.canceldate) as dCancelDate,
I will appreciate some assistant, my line is not correct for SQL Server 2012.
The decode formula is equivalent to
case sum(case when a.canceldate is null then 1 else 0 end) when 1 then null
else to_date( ... ) end dCancelDate, ...
One mistake I saw in your translation is that you have when sum(...) when 1. You can't have it both ways, it is either when sum(...) = 1 or sum(...) when 1. It may be the only mistake, I didn't look too hard.
What you have within the to_date() is horrible; are you converting dates to character strings, then take the max IN ALPHABETICAL ORDER and then translate back to date? Why? Perhaps just so you delete the time-of-day component? That is a lot easier done with trunc(max(a.canceldate)).

Conversion failed when converting the nvarchar to int

I have a field which is varchar and contains numbers and dates as strings. I want to update all numbers in this field that is greater than 720. I have attempted firstly to do a select but I get this error:
Conversion failed when converting the nvarchar value '16:00' to data type int.
This is my query:
select id, case(isnumeric([other08])) when 1 then [other08] else 0 end
from CER where sourcecode like 'ANE%' --and other08 > 720
It fails when I uncomment the last part.
I am trying to get all numerics greater than 720, but I can't do the comaprison. It also fails when casting and converting.
Thanks all for any help
You also need to perform the checks and conversion in the WHERE clause:
SELECT
id,
CASE WHEN isnumeric([other08]) = 1 THEN CAST([other08] AS INT) ELSE 0 END
FROM CER
WHERE sourcecode LIKE 'ANE%'
AND CASE WHEN isnumeric([other08]) = 1 THEN CAST([other08] AS INT) ELSE 0 END > 720
You need to use IsNumeric in your where clause, to avoid trying to compare strings to the number 720. Eg:
select id, case(isnumeric([other08])) when 1 then [other08] else 0 end
from CER
where sourcecode like 'ANE%' and ISNUMERIC(other08) = 1 and other08 > 720
EDIT
As #Abs pointed out, the above approach won't work. We can use a CTE to compute a reliable field to filter on, however:
WITH Data AS (
select id
, case WHEN isnumeric([other08]) THEN CAST([other08] AS int) else 0 end AS FilteredOther08
, CER.*
from CER
where sourcecode like 'ANE%'
)
SELECT *
FROM Data
WHERE [FilteredOther08] > 720

Resources