Oracle null check for string field - sql

In this below example why other than "NAME1" all giving null as result in oracle 11g.
If I mention space explicitly it takes space otherwise null only not empty string.Please help me to clarify this.
In NAME2 I specify the empty space but still it gives null.
select
NVL(NAME,' ') AS NAME1,
NVL(NAME,'') AS NAME2,
NVL(NAME,NULL) AS NAME3,
NAME AS NAME4
from employee
OUTPUT :
(space),null,null,null

Because in Oracle a ZERO length varchar is treated as NULL.
In your example
NVL(NAME, ' ') AS NAME1 will evaluate to either NAME or ' ' - empty string.
NVL(NAME, '') as NAME2 will evaluate to either NAME or a zero length string

Because '' is equal with null in oracle. For example these two queries are same:
update test set name='' where id = 15
update test set name=null where id = 15

The reason is that in Oracle an empty string is equivalent to NULL.
You can see that this is true by executing the followig query
SELECT 'x' FROM DUAL WHERE '' IS NULL
This should return a single row, meaning that the condition '' IS NULL is true.

Related

SQL Server CASE statement with multiple THEN clauses

I have seen several similar questions but none cover what I need. I need to put another THEN statement after the first one. My column contains int's. When it returns NULL I need it to display a blank space, but when I try the below code, I just get '0'.
CASE
WHEN Column1 IS NULL
THEN ''
ELSE Column1
END
If I try to put a sting after THEN then it tells me that it cannot convert it from int. I need to convert it to varchar and then change its output to a blank space afterwards, such as:
e.g.
CASE
WHEN Column1 IS NULL
THEN CONVERT(varchar(10), Column1)
THEN ''
ELSE Column1
END
Is there a way of doing this?
Thanks
Rob
A case expression returns a single value -- with a given type. If you want a string result, then you need to be sure that all paths in the case return strings:
CASE WHEN Column1 IS NULL
THEN ''
ELSE CAST(Column1 AS VARCHAR(255))
END
This is more simply written using COALESCE():
COALESCE(CAST(Column1 as VARCHAR(255)), '')
You cannot display an integer as a "blank" (other than using a NULL value).

SQL , SQL QUERIES

I have the following condition:
if column is NULL, or have value like Unassessed or ABC then the result should be I.
I need help how to put three in one condition like below. Just confused with NULL value. And it is string in column.
JobAssessStage ='Unassessed','','ABC'
JobAssessStage in ('Unassessed','','ABC')
Use below condition with is null
JobAssessStage in ('Unassessed','ABC') or JobAssessStage is null
use case when for multiple condition
select case when JobAssessStage in ('Unassessed','ABC') or
JobAssessStage is null then 'I' else 'You' End
from yourtable
In some cases you can use also use COALESCE function (it returns the first non-null expression in a list) on JobAssessStage field:
COALESCE(JobAssessStage, 'X') in ('Unassessed','ABC','X')
If JobAssessStage is NULL with COALESCE the value you're testing becomes X.
I told you "in some cases" because you have to be sure that the value you have chosen (X) is not a value that your field can take.
You can formulate your like below:
SELECT *
FROM table
WHERE 1 = 1
AND (JobAssessStage IN ('Unassessed', 'ABC')
OR JobAssessStage IS NULL);

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';

Concatenate with NULL values in SQL

Column1 Column2
------- -------
apple juice
water melon
banana
red berry
I have a table which has two columns. Column1 has a group of words and Column2 also has a group of words. I want to concatenate them with + operator without a space.
For instance: applejuice
The thing is, if there is a null value in the second column, i only want to have the first element as a result.
For instance: banana
Result
------
applejuice
watermelon
banana
redberry
However, when i use column1 + column2, it gives a NULL value if Comunm2 is NULL. I want to have "banana" as the result.
Use the COALESCE function to replace NULL values with an empty string.
SELECT Column1 + COALESCE(Column2, '') AS Result
FROM YourTable
A few posts I have made tagged MSSQL have been renamed to 'SQL' by a moderator. So I am assuming you are using MSSQL
COALESCE will return the FIRST non-null value.
SELECT COALESCE('a', NULL, 'c')
will only return 'a'
If you want Firstname + Lastname, where sometimes one or the other is NULL, use CONCAT. Concat adds the strings together and replaces NULLS with 0 length non-null value.
SELECT CONCAT('a', NULL, 'c')
will return 'ac'
If you want Fn space + middle name space + LN, combine concatinate with CONCAT:
SELECT CONCAT('a' + ' ', NULL + ' ', 'c')
Will return 'a c'.
The space after middlename (null) is eliminated with the + and NULL.
NULL + ' ' is null.
So in cases where Middlename or Firstname is null, you won't get extra unwanted spaces.
Standard SQL requires that string concatenation involving a NULL generates a NULL output, but that is written using the || operation:
SELECT a || b
FROM SomeTable;
The output will be null if either a or b or both contains a NULL.
Using + to concatenate strings indicates that you are using a DBMS-specific extension. The behaviour might be the same as the standard requires - indeed, that seems to be the gist of your question.
Some DBMS - notably Oracle - tend to treat null strings as equivalent to empty strings; then you can concatenate away merrily. However, that behaviour is not strictly standard-compliant if the || operator is used.
Consider using COALESCE or NVL or IFNULL or some similar function to map the NULL to an empty string before concatenating.
If you are using MySq, use ifnull(Column2, '')
I'm not certain what you're using as your database, but I would look for a "coalesce" function for your particular SQL dialect and use that.
The + sign for concatenation in TSQL will by default combine string + null to null as an unknown value.
You can do one of two things, you can change this variable for the session which controlls what Sql should do with Nulls
http://msdn.microsoft.com/en-us/library/ms176056.aspx
Or you can Coalesce each column to an empty string before concatenating.
COALESCE(Column1, '')
http://msdn.microsoft.com/en-us/library/ms190349.aspx
You can do a union:
(SELECT Column1 + Column2 FROM Table1 WHERE Column2 is not NULL)
UNION
(SELECT Column1 FROM Table1 WHERE Column2 is NULL);
You can use a case condition:
case when column_2 is not null
then concatenate
else column_1
end

Difference between MySQL IS NOT NULL and != ''

Is there any difference between MySQL
IF (myText IS NOT NULL) THEN
and
IF (myText != '') THEN
Yes there is a big difference between a NULL value and a blank/empty value.
Here's one resource that describes the differences.
When myText IS NULL:
myText IS NOT NULL evaluates to FALSE
myText != '' evaluates to NULL (which essentially behaves the same as FALSE would in this specific case you wrote)
However, you should not get into the habit of treating them the same, since most of the time they will behave differently: For example:
Assume you have a table tbl:
id text
1 NULL
2
3 abc
Note: 1 contains a NULL value, and 2 contains an empty string ('').
If you run the following query:
SELECT * FROM tbl WHERE text != ''
... it will return record 3.
If you run the following query:
SELECT * FROM tbl WHERE text IS NOT NULL
... it will return records 2 and 3.
Yes there is a difference.
In simple words, myText IS NOT NULL specifies that myText is having some value which could be '' too.
Where as myText != '' specifies that it returns TRUE, if myText does NOT contain an empty string.
There is a difference. If the default value of a column is "NULL", then if no data has been set for a field, it is truly null. However, if the value of a field has been updated as '', it is not NULL, rather it is empty.
See here for more information Link