How to convert Null to Empty String with SQL - sql

I want to convert Null data to an empty string. How can I do it?
Here is my column screenshot, All of them are null.
Here is my Stored Procedure code;
ISNULL(CustomerStatusId, '') AS StatusId
And here is my website screenshot, all of them are zero.

The problem is that the column has a number type of some sort. You need to convert to a string value, even if the value is NULL:
COALESCE(CAST(CustomerStatusId as VARCHAR(255)), '') AS StatusId

If your column value id 0 then instead of IsNull please use NULLIF and Coalesce(). NULLIF(CustomerStatusId,0) will return null which will be replaced by Coalesce() with ''.
Coalesce(nullif(CustomerStatusId,0), '') AS StatusId

Related

Is it possible to use a COALESCE function inside a SUM function?

I'm trying to use COALESCE function at postgres in a query like this:
SELECT
id_school,
SUM(COALESCE((student->'name'->>'age'), '0')::numeric)
FROM
teachers
GROUP BY 1;
The problem is at line:
SUM(COALESCE((student->'name'->>'age'), '0')::numeric)
I tried to use COALESCE because I'm receiving some empty strings and the ERROR: invalid input syntax for type numeric: ""
Shouldn't COALESCE solve this casting error? Can't I use COALESCE in a SUM function?
You don't actually need the coalesce() function inside the sum, because sum will ignore NULL values anyway.
You error isn't caused by a NULL value, but by an empty string.
So you need to convert the empty string to a NULL value. This can be done using nullif() in Postgres
SELECT id_school,
SUM(nullif(student->'name'->>'age'), '')::numeric)
FROM teachers
GROUP BY 1;
If you could also have strings with blanks in it the JSON value, you might want to consider nullif(trim(student->'name'->>'age'), '')
COALESCE returns first non-null argument. Empty string is non-null. You are trying to cast '' to numeric value.
You can use NULLIF to get null instead of empty string in COALESCE.
SQL Coalesce with empty string

What does this statement mean in SQL? (isnull(cast(field_name as CHAR), '') = ")

I am new to SQL and I am looking at established queries that we have, can anyone explain what this statement means in the WHERE clause
(isnull(cast(field_name as CHAR), '') = '').
Breaking down each statement:
CAST(field_name AS CHAR) converts the field_name column to a CHAR
This value is then passed as the first argument to the ISNULL() with the second being an empty string ''.
ISNULL(CAST(field_name AS CHAR), '')
This takes the result of the cast, and if it's a NULL value, returns '' instead.
Finally, it checks if that result is equal to ''.
Essentially, it's checking for NULL or empty string values in one fell swoop.
juergen d is correct. I'd add that it tries to cast the field to alphanumeric before it checks if it is null or blank. If the field's data type has no defined conversion to type CHAR, the query will error out. Have a look at this regarding conversions/casts.
T-SQL Data Types and Conversion Info

Check if column value is Numeric. SSIS

I have a column with datatype of varchar. I would like to replace all the values that are not numeric with NULL.
So for example my column can contain a value of MIGB_MGW but also 1352. The current expression I am using with Derived Column Transformation Editor is:
(DT_I4)kbup == (DT_I4)kbup ? 1 : 0
But of course this replaces all the values I want to keep with 1. What expression would I use to keep the numeric values? (1352 in this example)
If you want a null of varchar type, you can use NULL(DT_STR). For a DT_I4 you can use NULL(DT_I4) etc.
You can then use (DT_I4)kbup in place of your 1 to return the original varchar value that you want to keep, converted to a DT_I4:
(DT_I4)kbup == (DT_I4)kbup ? (DT_I4)kbup : NULL(DT_I4)
You could just convert them with a Derived Column and then use the ignore failure option in the Error output.
Use NOT LIKE
SELECT CASE
WHEN col NOT LIKE '%[^0-9]%' THEN col
ELSE NULL
END as Only_Numeric
FROM (VALUES ('MIGB_MGW'),
('1352')) tc(col)
Result :
Only_Numeric
------------
NULL
1352
Another option if 2012+ is Try_Convert()
SELECT Try_Convert(float,col)
FROM (VALUES ('MIGB_MGW'),
('2.6e7'),
('2.6BMW'),
('1352')) tc(col)
Returns
NULL
26000000
NULL
1352

Select ISNULL is not returning replacement value

ContactPerson type is nvarchar.
The issue is when ContactPerson is empty not returning n/a
Query
Select ISNULL(ContactPerson,'n/a') from [dbo].[Suppliers]
An empty string ('') and not the same thing as a null value. ISNULL() returns the replacement value only for an actual null value.
You can use NULLIF() to evaluate an empty string into a null value:
SELECT ISNULL(NULLIF(ContactPerson, ''), 'n/a') ...
I would suggest to use COALESCE instead of ISNULL, with NULLIF function:
SELECT COALESCE(NULLIF(ContactPerson,''),'n/a')
FROM [dbo].[Suppliers]
You can find the difference of COALESCE and ISNULL f.e. here.

Using Substring, within ISNULL in TSQL returns unexpected number of characters for the field

My field in my SKU table
(BI.dbo.SKU.phl5) is varchar(15)
However below code returns just 3 characters 'Unc' for the null fields in my table while it should return 'Uncategorized'. How to solve that?
ISNULL(SUBSTRING(BI.dbo.SKU.phl5,0,3),'Uncategorized') AS phl1
ISNULL(CAST(SUBSTRING(BI.dbo.SKU.phl5,0,3) AS VARCHAR(13)),'Uncategorized') AS phl1
The size of the return type of SUBSTRING isn't clearly documented that I can find, but the problem is that the type of ISNULL is the type of the first expression, which is clearly coming back as VARCHAR(3) since you are truncating it to 3 characters.
ISNULL docs
Try this
CASE WHEN BI.dbo.SKU.phl5 IS NULL THEN 'Uncategorized'
ELSE SUBSTRING(BI.dbo.SKU.phl5,0,3)
END AS phl1