I have the following SQL query
select s.comments + s.further_comments from dbo.samples s where id = 1234
However if s.comments or s.further_comments is NULL the whole string is returned NULL
How do I convert the NULL value to an empty string or at least only return the non NULL values in this string?
You can use either ISNULL or COALESCE for this.
SELECT ISNULL(s.comments, '') + ISNULL(s.further_comments, '')
SELECT COALESCE(s.comments, '') + COALESCE(s.further_comments, '')
ISNULL
Replaces NULL with the specified replacement value.
COALESCE
Returns the first nonnull expression among its arguments.
Note that there are some differences between the two methods but for all intents and purposes, they most likely don't apply to your situation.
ISNULL(NULL, NULL) -- is int
COALESCE(NULL, NULL) -- Will throw an error
COALESCE(CAST(NULL as int), NULL) -- it valid and returns int
ISNULL takes only 2 parameters whereas COALESCE takes variable number of parameters
COALESCE is based on the ANSI SQL standard whereas ISNULL is a proprietary TSQL function
Related
I have the following sub-select expression inside my query. What I want to do is wrap the entire aggregate expression with an ISNULL function, and when a NULL is encountered I want it to return an empty string ('').
I tried running the below but I get Varchar to Numeric conversion error because the empty string is varchar, but the expression is being calculated numerically.
SELECT PS_PO_LINE.PO_ID, PS_PO_LINE.PO_DT,
ISNULL((SELECT SUM (PS_PO_LN_ORDER_VW.AMT_ORDERED)
FROM PS_PO_LN_ORDER_VW PS_PO_LN_ORDER_VW WHERE PS_PO_LN_ORDER_VW.BUSINESS_UNIT=PS_PO_LINE.BUSINESS_UNIT AND
PS_PO_LN_ORDER_VW.PO_ID=PS_PO_LINE.PO_ID AND PS_PO_LN_ORDER_VW.LINE_NBR=PS_PO_LINE.LINE_NBR
AND PS_PO_LINE.AMT_ONLY_FLG = 'Y' ), '') AS "AMOUNT"
FROM PS_PO_LINE PS_PO_LINE
I don't want to return an 0 if the NULL function evaluates to True, I would rather return '' instead. Is this possible?
#Charlieface is right: ISNULL forces the second parameter into the first parameter's type.
Maybe try this
SELECT PS_PO_LINE.PO_ID, PS_PO_LINE.PO_DT,
ISNULL(CAST((SELECT SUM (PS_PO_LN_ORDER_VW.AMT_ORDERED)
FROM PS_PO_LN_ORDER_VW PS_PO_LN_ORDER_VW WHERE PS_PO_LN_ORDER_VW.BUSINESS_UNIT=PS_PO_LINE.BUSINESS_UNIT AND
PS_PO_LN_ORDER_VW.PO_ID=PS_PO_LINE.PO_ID AND PS_PO_LN_ORDER_VW.LINE_NBR=PS_PO_LINE.LINE_NBR
AND PS_PO_LINE.AMT_ONLY_FLG = 'Y' ) AS VARCHAR, '') AS "AMOUNT"
FROM PS_PO_LINE PS_PO_LINE
I am trying to concat the columns here but when I encounter a column with empty / blank string, the concat failed.
I need to do some formatting for each column with different data type, so I am not using the CONCAT function. Using the conventional way like
SELECT CONVERT(varchar, [Priority]) + '~' + CONVERT(varchar,[AP_type]) + '~' + [AP_Name] + '~'
FROM table
Any suggestions on how I can concat empty string ?
Results I am looking :
0~0~~~~In~In
Thanks.
Couple of things.
always best to specify the length when converting to varchar. For example varchar(50)
concat() will handle nulls as empty string and there is no need to convert. Oddly enough, char(0) creates the odd behavior.
Example
Declare #YourTable Table ([priority] varchar(50),[ap_type] varchar(50),[ap_name] varchar(50),[ap_par] varchar(50),[infoText] varchar(50),[TxtCame] varchar(50),[TxtWent] varchar(50))
Insert Into #YourTable Values
(0,0,'','','','In','In')
,(0,0,'','',null,'In','In') -- Has Null
,(0,0,'','',char(0),'In','In') -- Has Char(0) ... Truncates without NullIf()
Select NewString = concat(priority,'~',ap_type,'~',ap_name,'~',ap_par,'~',NullIf(infoText,char(0)),'~',TxtCame,'~',TxtWent)
from #YourTable
Returns
NewString
0~0~~~~In~In
0~0~~~~In~In
0~0~~~~In~In -- NullIf() was required to fix
Why the script below returns NULL instead of 0?
DECLARE #number BIGINT = 0;
SELECT NULLIF(#number, '');
According to the MSDN, it should return 0:
NULLIF
Returns a null value if the two specified expressions are equal.
For SQL server, 0 and '' is considered the same (=equal)? What is the logic behind?
When an operator combines two expressions of different data types, the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence.
SELECT CONVERT(bigint, '')
SELECT CONVERT(float, '')
SELECT CONVERT(date, '')
0
0
1900-01-01
https://learn.microsoft.com/en-us/sql/t-sql/data-types/data-type-precedence-transact-sql
As BOL states: "the rules for data type precedence specify that the data type with the lower precedence is converted to the data type with the higher precedence."
You've got two different datatypes, bigint and nvarchar. In order to compare the two, they have to be the same datatype. Following the rule described, the nvarchar is implicitly converted to bigint. Try select convert(bigint, ''), you'll find it results in 0. So they are the same.
This script should return null and it is true!
The reason behind it is '' is a string, so it will get implicitly casted to an integer value when comparing it with an integer as you are doing now!
In general, you're asking for trouble when you're comparing values of different data types, since implicit conversions happen behind the scene.
This is the result of implicit conversion. In some cases a string value can be converted to an integer (such as empty string is converted to 0).
Essentially SQL Server tries to match the data type of the two expressions first, then it checks the values.
DECLARE #number BIGINT = 0;
SELECT
CONVERT(BIGINT, '')
, NULLIF(#number, '')
, NULLIF(#number, CONVERT(BIGINT, ''))
It has converted '' to the integer which is 0, as integer has higher precedence in data type. Check the example below how '' become 0
SELECT CONVERT(INT, '') -- 0
SELECT CAST('' AS INT) -- 0
Hi please help me. How join 3 columns with null values?.
SELECT [item],[Prox],[z], [item]+[Prox]+[z] as result FROM [FIELD$];
Result.
Try this:
SELECT [item],[Prox],[z], COALESCE([item],'')+COALESCE([Prox],'')+COALESCE([z],'') as result
FROM [FIELD$];
Explanation:
COALESCE evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.
i.e., If [item] is NULL, then COALESCE([item],'') will return an empty string.
Other alternatives:
Instead of COALESCE(ColName,''), you can use:
ISNULL(ColName,'') for SQL Server.
IFNULL(ColName,'') for MySQL.
NVL(ColName,'') for Oracle.
As concatenating multiple strings with at least one null value results in NULL you may use coalesce to solve this:
SELECT
[item],
[Prox],
[z],
coalesce([item], '') + coalesce([Prox], '') + coalesce([z], '') as result
FROM
[FIELD$];
coalesce is ANSI standard and available in almost all reasonable databases.
IN SQL Server 2012, you can use CONCAT function:
SELECT [item],[Prox],[z], concat([item],[Prox],[z]) as result FROM [FIELD$];
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