PostgreSQL concatting multiple cases gives null as result - sql

I'm pretty new to PostgreSQL. The next query works in Oracle but won't work in PSQL:
select case 1 when null then null else 1||' ' end ||
case 2 when null then null else 2||' ' end ||
case 3 when null then null else 3 end as total
from numbers
If 1 and 2 are not null but only 3 is null, the result of total will be null despite the fact that 1 en 2 are not null and the result of total should not be null.
The end result should be that when one of 3 columns is not null, total should not be null. Is that possible?

For Postgres - unlike Oracle - an empty string '' and null are two different things. In Oracle a string concatenation with null treats the null value as an empty string. However when you store an empty string ('') in Oracle it treats it as a null value.
In SQL, all (or nearly all) operators are defined such that if any argument is null the result is also null, e.g. 42 + null or 'foo'||null.
Also case 1 when null makes no sense. 1 is never null and thus the when part would never be executed. Additionally you can't test for null that way. You need to use is null but then you can't use the abbreviated case syntax.
You probably meant to write something like this:
select case when first_column is null then '' else first_column||' ' end ||
case when second_column is null then '' else second_column||' ' end ||
case when third_column is null null then '' else third_column end as total
from numbers
However you can make this a lot easier using coalesce():
select coalesce(first_column, '')||' '||coalesce(second_column, '')||' '||coalesce(third_column, '')
or use concat() which treats null as an empty string:
select concat(first_column, ' ', second_column, ' ', third_column)
or even simpler use concat_ws() "concat with separator":
select concat_ws(' ', first_column, second_column, third_column)
concat_ws() will put the separator (the first parameter) between every value of the other parameters, but treats null values and empty strings properly, so that the separator does not occur twice between two values.
concat('one', ' ', null, ' ', '') returns 'one '
but concat_ws(' ', 'one', null, '') will return 'one'

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

Removing specified character from string in SQL

Say I have a string looking like this ",LI,PA,LK";
I want to remove the first char, so it looks like "LI,PA,LK";
In Java my code to handle this, will look like this:
public String returnSubs(String val) {
int index = val.indexOf(",");
String res = val.substring(index+1, val.length());
return res;
}
I want to achieve the exact same thing in SQL, having this query
select patientID, case when liver is not null then 'LI' else '' end
|| case when kidney_r is not null then ',KR' else '' end
|| case when kidney_l is not null then ',KL' else ''end
|| case when heart is not null then ',HE' else '' end
|| case when liver_domino is not null then ',LI-Dom' else '' end
|| case when lung_r is not null then ',LungR' else '' end
|| case when pancreas is not null then ',PA' else '' end
|| case when liver_split is not null then ',Lsplit' else '' end
|| case when lung_l is not null then ',LungL' else '' end
|| case when intestine is not null then ',Intestine' else '' end
into organType
from offers
where patientID > 1
;
Also, the string I get from the query above, could look like LI, PA, KL, (notice the comma is at the end, and not the begining)
I see that I can use the SUBSTRING and/or INSTR of SQL. But I'm not really sure how. I am creating a procedure where this will be handled
Thanks for any help
Oracle has a function trim() that does exactly what you want:
trim(leading ',' from col)
You can use this in either an update or select.
Note: You appear to be storing multiple values in a comma-delimited list. That is a very bad way to model data. You do not want to overload what strings are by storing multiple values. Oracle has many better alternatives -- association tables, nested tables, JSON, and XML come to mind.
You could also use LTRIM here:
SELECT
LTRIM(organTypes, ',') AS col_out
FROM offers;
Some databases, such as MySQL, offer functions like CONCAT_WS which concatenate with a separator while ensuring that no dangling separators are added to the resulting output. Oracle does not have this, but LTRIM should be sufficient here.
even this will work:
substr(',LI,PA,LK',2)
In SQL SERVER:
SUBSTRING(VAL,2,LEN(VAL))
VAL--> COLUMN NAME
2--> IT SKIPS 1ST VALUE
LEN-->LENGTH OF THE COLUMN

Oracle null check for string field

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.

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

TSQL CASE LTRIM (RTRIM NULL

SQL Syntax is still something I am learning. I am getting the error noted below the this snippet of code.
SELECT
CASE WHEN LTRIM(RTRIM(cLehmanNo)) =' ' THEN NULL
WHEN cLehmanNo IS NOT NULL THEN REPLACE ( cLehmanNo,SUBSTRING (cLehmanNo,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cLehmanNo),1), ' ' )
END asLOAN_NUMBER
,CASE WHEN LTRIM(RTRIM(cMERS)) =' ' THEN NULL
WHEN cMERS IS NOT NULL THEN REPLACE ( cMERS,SUBSTRING (cMERS,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cMERS),1), ' ' )
END asMERS_ID
and 100+ more of same.
Msg 8133, Level 16, State 1, Line 1
None of the result expressions in a CASE specification can be NULL.
What am I doing wrong? How do I keep the gist of the statement and not get this crazy error?
This happens when it can't infer the type.
e.g.
SELECT CASE WHEN 1 = 2 THEN NULL ELSE NULL END
But this works
SELECT CASE WHEN 1 = 2 THEN NULL ELSE replace(NULL,'','') END
so I doubt the error is from the code you have shown us (You are using string functions and the following quick test shows that it will assume that to be varchar(8000))
SELECT CASE WHEN 1 = 2 THEN NULL ELSE REPLACE(NULL,'','') END a
INTO t /*Creates column of datatype varchar(8000)*/
You need to convert NULL to a correct type matching the overall values, e.g. CONVERT(VARCHAR(10), NULL), otherwise the server can't deduce which type to make the resulting value.
The error message actually means that all results in one of your case expressions are null. You have an expression like:
case when something then null when something then null end
At least one of the results has to be something other than null. You could circumvent this, but most likely there is a mistake in the query, as a case exression that always returns the same result is pointless.
The error message has been changed to:
At least one of the result expressions
in a CASE specification must be an
expression other than the NULL
constant.
SELECT
CASE WHEN LTRIM(RTRIM(cLehmanNo)) =' ' THEN NULL
WHEN cLehmanNo IS NOT NULL THEN REPLACE ( cLehmanNo,SUBSTRING (cLehmanNo,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cLehmanNo),1), ' ' )
ELSE ''
END asLOAN_NUMBER
,CASE WHEN LTRIM(RTRIM(cMERS)) =' ' THEN NULL
WHEN cMERS IS NOT NULL THEN REPLACE ( cMERS,SUBSTRING (cMERS,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cMERS),1), ' ' )
ELSE ''
END asMERS_ID