TSQL CASE LTRIM (RTRIM NULL - sql

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

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

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).

PostgreSQL concatting multiple cases gives null as result

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'

SELECT CASE WHEN TINYINT field condition met return string

I am using SQL Server 2005. I am trying to do a select from a TINYINT field. If the field's value is 0 I want to select an empty string instead ' ' . The fields name is level. HEre is what I am doing:
SELECT
[Level] =
Case
t.[Level]
WHEN 0 THEN ' '
ELSE t.[Level]
END
FROM table t
This code always returns 0. I was trying to troubleshoot the issue and tried this:
SELECT
[Level] =
Case
t.[Level]
WHEN 0 THEN 'test'
ELSE t.[Level]
END
FROM table t
And I got a the error Conversion failed when converting the varchar value 'test' to data type tinyint
So I'm seeing that there is a conversion problem here. I've tried:
SELECT
[Level] =
Case
t.[Level]
WHEN 0 THEN CONVERT(VARCHAR,t.[level])
ELSE t.[Level]
END
FROM table t
But this of course still returns 0, just the character, so it's still not doing what I need. I am thinking that there is most likely a better way to do this but am not sure how to approach it. Could anyone give me some advice on how to handle this? Thanks much!
For CASE statements, all returned values must be of the same type (or automatically convertable); that's the reason why '' was working, but 'test' wasn´t.
SELECT [Level] =
Case t.[Level]
WHEN 0 THEN ' '
ELSE CONVERT(VARCHAR(3), t.[Level])
END
FROM table t
Your attempt to fix this went the wrong way; you needed to convert the else portion to a varchar to match the varchar empty string:
SELECT
[Level] =
Case
t.[Level]
WHEN 0 THEN ''
ELSE CAST(t.[Level] AS VARCHAR(10))
END
FROM table t