Visual Studio 2008 Case Expression using null - sql

I'm trying to right a report that lets a user select either 'all values including null' or 'all values excluding null'. I placed a case expression in the where section. It doesn't work correctly for two reasons:
I can't pull the values of null and is not null at the same time.
In both cases it ends up pulling all values including null for the first one and only null values for the second.
Where
((CASE WHEN #nullvalue = 1 THEN check_rtn_void_dte end is not null) OR (CASE WHEN #nullvalue = 2 THEN check_rtn_void_dte end is null))
Is there a operator I can use that will pull null and is not null in one statement? Also, why is the first case pulling all data including null values?

Perhaps something like this would be simpler...
SELECT *
FROM MyTable
WHERE (#nullvalue = 2 OR check_rtn_void_dte IS NOT NULL)
That should pull out all results if #nullvalue = 2, or only non-NULL values if #nullvalue is something else.

Related

Select column, if blank select another column else ' ' if BOTH are null

Using SQL Server, I would like to have a field print out as blank rather than 'null'. The query is to first pick up phone number from one table - if null then it picks up from another table. If both are null, then I would like to have it return ' ' (blank) rather than 'null'.
I have tried multiple things in the 2nd line but still get nulls instead of blanks to print.
SELECT case when dbo.vwPersonDistinctPhone.phone IS NULL THEN PERSONAL_PHONE_NUMBER
when (dbo.vwPersonDistinctPhone.phone is null and PERSONAL_PHONE_NUMBER is null) then ' '
else dbo.vwPersonDistinctPhone.phone END AS 'phone',
i dont think you need the second when, you should be able to just to do this, there is a isnull function in sql server that if the value is null then it replaces it with the second parameter. So in this case if personal_phone_number is null then it will be '' or personal_phone_number if is not.
SELECT case when dbo.vwPersonDistinctPhone.phone IS NULL THEN ISNULL(PERSONAL_PHONE_NUMBER, '') else dbo.vwPersonDistinctPhone.phone END AS 'phone',

NULL values not filtered out with WHERE statement

SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE
When I select these columns, I see that the results have some NULL values. They don't seem to be strings. However, when I try to filter the NULL values out with a where statement:
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE VOLUME = NULL
I don't see any results. What might be the possible causes? I also tried filtering with 'NULL' but that would throw an error since the column type is double.
use this for only want null recode
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE VOLUME IS NULL
or
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE ISNULL(VOLUME,'') = ''
if you get not null value then use
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE ISNULL(VOLUME,'') <> ''
or
SELECT ID, VOLUME, TYPEOF(VOLUME) FROM DBT.BASE WHERE VOLUME IS NOT NULL
a more full answer is NULL is not directly comparable, much like NaN is not comparable in floating point numbers. Both represent the "lack of a value" if you "have not value here" how can you compare it to something.
"There is nobody next to you, what is their name?" it just doesn't make sense.
So to test you ask column IS NULL or column IS NOT NULL or you can use a compact logic expression see Conditional Expressions but some common ones in Snowflake are:
short form
ANSI long
snowflake long
NVL(column,'')
CASE WHEN column IS NOT NULL THEN column ELSE '' END
IFF(column IS NOT NULL, column, '')
NVL2(column,'a','b')
CASE WHEN column IS NOT NULL THEN 'a' ELSE 'b' END
IFF(column IS NOT NULL, 'a', 'b')
ZEROIFNULL(column)
CASE WHEN column IS NOT NULL THEN column ELSE 0 END
IFF(column IS NOT NULL, column, 0)
COALESCE/NVL/IFNULL are all interchangable so I will only show one (expect COALESCE can handle N items which are checked in order)
You can use the where is function or is not function to filter all the null values.

Invalid argument for function integer IBM DB2

I need to filter out rows in table where numer_lini column has number in it and it is between 100 and 999, below code works just fine when i comment out line where i cast marsnr to integer. However when i try to use it i get error: Invalid character found in a character string argument of the function "INTEGER". when looking at the list seems like replace and translate filters only numbers just fine and select only contains legit numbers (list of unique values is not long so its easy to scan by eye). So why does it fail to cast something? I also tried using integer(marsnr), but it produces the same error. I need casting because i need numeric range, otherwise i get results like 7,80 and so on. As I mentioned Im using IBM DB2 database.
select numer_lini, war_trasy, id_prz1, id_prz2
from alaska.trasa
where numer_lini in (
select marsnr
from (
select
distinct numer_lini marsnr
from alaska.trasa
where case
when replace(translate(numer_lini, '0','123456789','0'),'0','') = ''
then numer_lini
else 'no'
end <> 'no'
)
where cast(marsnr as integer) between 100 and 999
)
fetch first 300 rows only
If you look at the optimized SQL from the Db2 explain, you will see that Db2 has collapsed your code into a single select.
SELECT DISTINCT Q2.NUMER_LINI AS "NUMER_LINI",
Q2.WAR_TRASY AS "WAR_TRASY",
Q2.ID_PRZ1 AS "ID_PRZ1",
Q2.ID_PRZ2 AS "ID_PRZ2",
Q1.NUMER_LINI
FROM ALASKA.TRASA AS Q1,
ALASKA.TRASA AS Q2
WHERE (Q2.NUMER_LINI = Q1.NUMER_LINI)
AND (100 <= INTEGER(Q1.NUMER_LINI))
AND (INTEGER(Q1.NUMER_LINI) <= 999)
AND (CASE WHEN (REPLACE(TRANSLATE(Q1.NUMER_LINI,
'0',
'123456789',
'0'),
'0',
'') = '') THEN Q1.NUMER_LINI
ELSE 'no' END <> 'no')
Use a CASE to force Db2 to do the "is integer" check first. Also, you don't check for the empty string.
E.g. with this table and data
‪create‬‎ ‪TABLE‬‎ ‪alaska‬‎.‪trasa‬‎ ‪‬‎(‪numer_lini‬‎ ‪VARCHAR‬‎(‪10‬‎)‪‬‎,‪‬‎ ‪war_trasy‬‎ ‪INT‬‎ ‪‬‎,‪‬‎ ‪id_prz1‬‎ ‪INT‬‎,‪‬‎ ‪id_prz2‬‎ ‪INT‬‎)‪;
insert into alaska.trasa values ('',1,1,1),('99',1,1,1),('500',1,1,1),('3000',1,1,1),('00300',1,1,1),('AXS',1,1,1);
This SQL works
select numer_lini, war_trasy, id_prz1, id_prz2
from alaska.trasa
where case when translate(numer_lini, '','0123456789') = ''
and numer_lini <> ''
then integer(numer_lini) else 0 end
between 100 and 999
Although that does fail if there is an embedded space in the input. E.g. '30 0'. To cater for that, a regular expressing is probably preferred. E.g.
select numer_lini, war_trasy, id_prz1, id_prz2
from alaska.trasa
where case when regexp_like(numer_lini, '^\s*[+-]?\s*((\d+\.?\d*)|(\d*\.?\d+))\s*$'))
then integer(numer_lini) else 0 end
between 100 and 999

IF Inside WHERE with stored procedure

I have a WHERE statement that looks like this:
WHERE
((#Value1 IS NULL AND [value1_id] IS NULL) OR [value1_id] = ISNULL(#Value1, [value1_id]))
AND
((#Value2 IS NULL AND [value2_id] IS NULL) OR [value2_id] = ISNULL(#Value2, [value2_id]))
AND
((#Value3 IS NULL AND [value3_id] IS NULL) OR [value3_id] = ISNULL(#Value3, [value3_id]))
AND
((#Value4 IS NULL AND [value4_id] IS NULL) OR [value4_id] = ISNULL(#Value4, [value4_id]))
AND
((#Value5 IS NULL AND [value5_id] IS NULL) OR [value5_id] = ISNULL(#Value5, [value5_id]))
AND
((#Value6 IS NULL AND [value6_id] IS NULL) OR [value6_id] = ISNULL(#Value5, [value6_id]))
I need to add some conditional logic inside of the WHERE so I can do special things with Value5 and Value6. Basically, if Value5, Value6, or another value is null, I want to use the Value 5 and Value 6 lines as is. If all three values aren't NULL, I need to run some calculations on the values.
Any ideas on what the best action would be?
Use CASE
"CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING."
http://msdn.microsoft.com/en-us/library/ms181765.aspx
You can perform calculations inside of a SELECT like this:
SELECT
CASE
WHEN #Value7 is not null THEN #Value7 * 100
ELSE Value7 * 100
END
FROM #T
WHERE (#Value1 is null and Value1 is null)
AND (#Value2 is null and Value2 is null)
It's still not very clear what exactly you're trying to accomplish. Is the WHERE not inside a SELECT? Provide a clear, concise example if the above is not correct.
Basically, if Value5, Value6, or another value is null, I want to use
the Value 5 and Value 6 lines as is. If all three values aren't NULL,
I need to run some calculations on the values.
I think this will be hard to do with for example a CASE inside the WHERE clause. You might be better off with an IF construction outside of your SELECT statements:
IF (#Value5 IS NULL OR #Value6 IS NULL OR #OtherValue IS NULL)
BEGIN
<Statement1AsIs>
END
ELSE
BEGIN
<Statement2WithComputations>
END
Rewrite WHERE clause
On a short sidenote, I know you did not ask for it, but I have the feeling there must be a 'clearer' way to write these
((#Value1 IS NULL AND [value1_id] IS NULL) OR [value1_id] = ISNULL(#Value1, [value1_id]))
I've been playing around a bit and this is one way to say the same:
COALESCE(#Value1, [value1_id], 1001) = COALESCE([value1_id], 1001)
This is another one, equivalent and shorter:
#Value1 IS NULL OR [value1_id] = ISNULL(#Value1, [value1_id])
Check out this SQL Fiddle to see the equivalence.

Oracle no results, small query

I am having trouble with some sql. When I run the following query:
Select * from teleapp;
I get TONS of results. Resulst which include a column (called cashwithappyn) that has TONS of empty or null data cells. (They look empty and don't say null)
The column info is:
ColumnName ID Null? Data Type Histogram Num Distinct Num Nulls Density
CASHWITHAPPYN 54 Y VARCHAR2(1 Byte) Frequency 2 56895 0
When I try to run the following query:
Select * from teleapp where cashwithappyn = null;
or
Select * from teleapp where cashwithappyn = '';
or
Select * from teleapp where cashwithappyn not like '';
or
Select * from teleapp where cashwithappyn not in ('Y','N');
or ANY type of combination, I cannot seem to get all of the rows with nothing in cashwithappyn.
Any ideas? Please help, this is the last part of a project that I was assigned to do and I just need to figure this out.
Thanks.
Maybe the column contains blanks. In that case you can do
WHERE TRIM(CASHWITHAPYYN) IS NULL
TRIM removes all blanks before and after and if nothing is left anymore the value becomes NULL
e.g.
TRIM(' ') IS NULL -- one blank removed = true
TRIM(NULL) IS NULL -- true
Also NULL cannot be compared with = NULL but must be phrased IS NULL. NULL is not a value as such and that is why the comparison never works.
You need to use IS NULL
Select * from teleapp where cashwithappyn is null;
Logical test expressions (=, Not In, Like etc) with null result in a false so all of the following result in false
1 = NULL
1 <> NULL
NULL = NULL
NULL <> NULL
NULL NOT IN ('a','b')
NULL Not Like NULL
Additionally in oracle the zero length string is null so NOT LIKE '' will never return any rows
You'll need to use either is null, is not null or Coalesce
A co-worker and I did a bunch of research, here's what we came up with that will work. Any ideas why this works but not the others?
Select * from teleapp where nvl(cashwithappyn,'U') = 'U';