SQL ISNULL not working - sql

I'm trying to display 00001 when the query result is null but the query still return null. I don't know whats wrong with my query.
EDIT:
Assuming OBRNo is 123-5678-10-13-1619 means LEN(a.OBRNo) is 19
SELECT TOP 1 CASE WHEN RIGHT(a.OBRNo, 5) = NULL THEN '00001' ELSE a.OBRNo
END as CaseWhen,
ISNULL(a.OBRNo, '00001') as ISNULL,
RIGHT(OBRNo, 5) as OrderBy
FROM tbl_T_BMSCurrentControl as a
WHERE LEN(a.OBRNo) = 20 and a.ActionCode = 1
ORDER BY OrderBy DESC

Compare with NULL with IS NULL / IS NOT NULL, not with = NULL.
SELECT TOP 1 CASE WHEN RIGHT(a.OBRNo, 5) IS NULL THEN '00001' ELSE a.OBRNo END
...
You could change this behaviour with SET ANSI_NULLS.
The reason why you can't compare with = by default is: NULL means undefined. Nothing is equal to unknown not even NULL. If you compare with NULL the result is unknown, hence also NULL.

Is your query returning any row?
Your ISNULL (x,y) should do what you expect but it looks like your WHERE is filtering all the records, due to the NULLS.
Try this:
SELECT TOP 1 CASE WHEN RIGHT(a.OBRNo, 5) = NULL THEN '00001' ELSE a.OBRNo
END as CaseWhen,
ISNULL(a.OBRNo, '00001') as ISNULL,
RIGHT(OBRNo, 5) as OrderBy
FROM tbl_T_BMSCurrentControl as a
WHERE (a.OBRNo IS NULL OR LEN(a.OBRNo) = 20) and a.ActionCode = 1
ORDER BY OrderBy DESC
LEN(a.OBRNo) being a.OBRNo NULL will be NULL so NULL = 20 will be NULL and NULL AND a.ActionCode = 1 will be NULL which when filtering is treated as FALSE

Related

Check multiple columns with value NULL in SQL Server

Currently I have the following sql statement:
SELECT CASE
WHEN sd.GV01 IS NULL
AND sd.GV02 IS NULL
AND sd.GV03 IS NULL
AND sd.GV04 IS NULL
AND sd.GV05 IS NULL
AND sd.CountryId = '4' THEN 4
END AS rating
FROM masterData sd
I tried to take the same way with a more short definition:
SELECT CASE
WHEN NULL IN (sd.GV01, sd.GV02, sd.GV03, sd.GV04, sd.GV05)
AND sd.CountryId = '4' THEN 4
END AS rating
FROM masterData sd
But I don't get return values, why? Can someone give me a better solution?
Your method is fine. Almost any comparison of NULL values returns NULL -- which is treated as false.
You could use COALESCE():
SELECT (CASE WHEN COALESCE(sd.GV01, sd.GV02, sd.GV03, sd.GV04, sd.GV05) IS NULL AND
sd.CountryId = '4'
THEN 4
END) AS rating
FROM masterData sd;

How to check if all rows validate a predicate

I've a table in my database for which I need to check if all rows have one field not null.
If there are no row or if there is at least 1 row with the field null => true
If there are rows and they are all with the field not null => False
Is there a way to do this in on simple query? Or I need to check if my table is empty first then if it's not check if I've a row with the field value empty ?
This will count how many NULL values you have in a field;
SELECT
SUM(CASE WHEN FieldName IS NULL THEN 1 ELSE 0 END) NullValues
FROM TableName
Will return 0 if there are no NULL values, and will return the number of NULLS if there are any present.
If you actually want to return a value as 'True' or 'False' then do this;
SELECT CASE
WHEN a.NullValues > 0
THEN 'True'
ELSE 'False'
END CheckField
FROM (
SELECT
SUM(CASE WHEN FieldName IS NULL
THEN 1
ELSE 0
END) NullValues
FROM TableName
) a
Use count(*) and count(field) and compare the two:
select
case when count(*) > 0 and count(*) = count(field) then 1 -- not empty and no nulls
else 0 end as isgood
from mytable;
Oracle SQL has no boolean data type , so I use 1 for true and 0 for false. You can replace this with whatever you like (e.g. 'true' instead of 1 and 'false' instead of 0).
As to turning this into a predicate (correlated to a main query), you'd use something along the lines of:
select ...
from main
where exists
(
select 1
from mytable
where mytable.colx = main.coly
having count(*) > 0 and count(*) = count(field)
);
You can do this with aggregation. However, it is difficult to understand what you are asking for. If you want to check that a field has no NULL values, you can do:
select (case when count(*) > 0 then 1 else 0 end) as HasNullValues
from t
where field is null;
Alternate way I found using max with putting null first:
select case when
max(field) keep (dense_rank first order by datfin desc nulls first) is null then 1
else 0 end as flag
from MYTABLE;

Sql Server 2008: How to order column with null values to show up in the end

I am currently not able to sort the column to show up null values in the end
SELECT firstPart,secondPart FROM Exhibit_table d, ExhibitType a WHERE d.case_id ='13-05'
AND d.ExhibitTypeId = TypeId AND d.ComplianceNo = '0' and active = 1 order by CONVERT(INT, firstPart), secondPart
I have 2 columns firstpart and secondpart I need to sort it such a way that it shows in the following order
10
11
12 A
12 B
12 C
null null
null null
Any help is greatly appreciated
You can add a third sorting condition:
SELECT firstPart,secondPart
FROM Exhibit_table d, ExhibitType a
WHERE d.case_id ='13-05' AND d.ExhibitTypeId = TypeId AND d.ComplianceNo = '0' and active = 1
ORDER BY CASE WHEN firstPart IS NULL AND secondPart IS NULL THEN 0 ELSE 1 END,
CONVERT(INT, firstPart), secondPart
This will introduce a calculated field with value of 0 when both fields are NULL and value of 1 otherwise - and sort by that field. You can adjust that condition as needed.

SQL, Select if or select case

I am not sure what I am trying is achievable or not!!
I am trying to write a SQL query will will do select statement based on user input.
so if user input = 1 then I want it to select from actual table.
if user input = 0 then I want it do select 0 or null from dual. (if this is possible).
so Here is Parameter which will used to get input from user. ?i_userkey:'':null?
if user input's 1 then it will change null to 1.
I want to write a query using this parameter. something like this.
below is the logic.
IF i_userkey = 1 then
select ID,Gender,Age from TableA
If i_userkey = 0 then
select 0 or null from dual.
is this possible?
How about this:
SELECT
CASE WHEN i_userkey = 1 THEN ID ELSE NULL END AS ID
CASE WHEN i_userkey = 1 THEN Gender ELSE NULL END AS Gender
CASE WHEN i_userkey = 1 THEN AGE ELSE NULL END AS Age
FROM TableA
This will at least give you a consistent three-column result set you can work with. Having the query return differing column counts is not going to work.
select ID,Gender,Age
from TableA
where i_userkey = 1
union all
select 0, 0, 0
from dual
where i_userkey = 0
You might have to adjust the datatypes in the dual-select to match TableA

<> operator in SQL

I have a table like this
ID Name IsDeleted
1 Yogesh Null
2 Goldy 1
Now when I run this query
select *
from tableName
where IsDeleted <> 1
I should get ID 1 record, But I am not getting it,
But when I run this
select *
from tableName
where IsDeleted is null
I get ID 1 record,
Why am I facing this behavior ??
Isn't NULL <> 1 is a true statement in SQL ??
IsDeleted is a bit type field with Allow Null true
select * from table
where COALESCE(IsDeleted, 0) <> 1
-- or ISNULL instead of COALESCE.
--ISNULL seems to be better in subqueries, but it's not ANSI SQL.
or
select * from table
where IsDeleted <> 1 or IsDeleted IS NULL
Comparing something with null will always result in unknown. That is why you need to use the is operator to compare null or use functions like COALESCE or isnull to replace null
select *
from tableName
where isnull(IsDeleted,0) <> 1
you compare different types. in this case its an other type (unknown) and not comparable
use the or statement to compare each type seperate
WHERE IsDeleted <> 1 OR IsDeleted is null
Learn about NULL - a comparison with NULL (in standard SQL) yields UNKNOWN, which is not true nor false (and the reason why your expectation is not met).
Try this:
PRINT CASE
WHEN 1 = NULL THEN '1 = NULL'
WHEN 1 <> NULL THEN '1 <> NULL'
ELSE '1 is neither = NULL nor <> NULL'
END
You can either first make sure that you don't have a NULL value (for instance by using the ISNULL or COALESCE functions), or use a condition with the operator IS NULL or IS NOT NULL.
Normal practice would dictate that if you had a column that essentially was a true false, yes no type of field then you should use a bit field with the default value set to 0.
So in your case above you could just run this:
select *
from tableName
where IsDeleted = 0
But in answer to your above question, if the Null is a true NULL value in the table then this will work for you:
select *
from tableName
where IsDeleted is null
or
select *
from tableName
where isnull(IsDeleted,0) = 0
to get record 1 and
select *
from tableName
where IsDeleted is not null
to get record 2
Good luck
Paul.