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

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

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: How to make a replace on the field ''

I have a very but tricky question for you guys. So, listen I have a field with spaces and numbers in one of my table columns. The key part is transform the content in a decimal field. The drawback is basically that for some rows I could get something like:
' 1584.00 '
' 156546'
'545.00 '
' '
So, to clean up my column, I have done a LTRIM and RTRIM so spaces gone. So now for a couple of records where the record were just spaces the new content is ''. Finally I need to convert this result to a decimal.
Issue: The thing is that for field that contend just the spaces the new result is '' and I'm not able to apply a REPLACE on this because it's a blank and the code below doesn't work:
SELECT REPLACE('','','0')
-- Final current verison
SELECT CAST(COALESCE(REPLACE(REPLACE([Gross_Weight],' ','0'),',',''),'0') AS DECIMAL(13,3))
How could I figure it out?
thanks so much
SELECT COALESCE(NULLIF(MyColumn, ''), 0)
This has the side-effect that you will also turn NULL values into 0, which you might not want. If that's a problem then a simple CASE statement should do the trick:
SELECT CASE WHEN MyColumn = '' THEN 0 ELSE CAST(MyColumn AS DECIMAL(10, 4)) END
Obviously you'll also have to incorporate any other manipulations that you're already doing.
No need for replace, just concatenate a zero to your column, like
SELECT RTRIM('0' + LTRIM(column))
I presume your data is in a table.
Lets call this table 'DATA' and the column 'VALUE'
Then you might use the below query
UPDATE DATA SET VALUE = 0 where VALUE = ''
To select the value do the below
select case ltrim(rtrim([Gross_Weight])) when ''
THEN 0
ELSE ltrim(rtrim([Gross_Weight])) END
Let me know if i get the requirement wrong.

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'

Visual Studio 2008 Case Expression using null

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.

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