Exclude Null Entries when concatenate - sql

I have columns
Col1|Col2|Col3
Aaaa|Bbbb|Cccc
Null|Bbbb|Cccc
Aaaa|Bbbb|Null
My result should look like
Aaaa,Bbbb,Cccc
Bbbb,Cccc
Aaaa,Bbbb
I tried this with the common '+' way of concatenation like
Select Col1 + ', ' + Col2 + ', ' + Col3
but with this statement I do get Nulls in the resultset if there is a Null in one of the columns. I could do something like
ISNULL(Col1, ' ')
but this end up in having ',' signs messing around
Can anyone help me out here?

The CONCAT() function solves your problem:
Select CONCAT(Col1, ', ', Col2, ', ', Col3)
It ignores NULL values.
The most recent versions of SQL Server support CONCAT_WS():
CONCAT_WS(', ', col1, col2, col3)
This produces slightly different results. For instance, if the second two values are NULL, the first returns 'A,,' but the second returns 'A'. I'm not sure which you really want.

Related

Concatenate SQL columns with comma separated

Is there any alternate way to concatenate SQL columns with comma separated. I am using below logic for concatenation. The columns (col1,col2,col3) can have null values.
select
stuff(
left(concat(col1,',',col2,',',col3),
len(concat(col1,',',col2,',',col3)) -
patindex('%[^,]%',reverse(concat(col1,',',col2,',',col3)))+1
)
,1,
patindex('%[^,]%',concat(col1,',',col2,',',col3))-1,''
)
from mytable
Sample data/output
In more recent versions of SQL Server you can use concat_ws():
select concat_ws(',', col1, col2, col3)
In earlier versions, there are various approach. A pretty simple one is:
select stuff( concat(',' + col1, ',' + col2, ',' + col3), 1, 1, '')
You can concat separators conditionally. This will output an empty string if either of the columns are null or empty.
select concat(col1,
case when len(col2)>1 then ',' else '' end,
col2,
case when len(col3)>1 then ',' else '' end,
col3)
from your_table;
To output null if either of the columns are null or empty, wrap the concat inside a nullif like this
select nullif(concat(col1,
case when len(col2)>1 then ',' else '' end,
col2,
case when len(col3)>1 then ',' else '' end,
col3),'')
from your_table;

t sql records that contain part of another record

Using SQL Server 2014. Is there a way to select records where the string value partially exists in another field?
e.g.:
RowID Field1 Field2
1 ABC ABC DEF
2 XYZ WERQ
3 MNB MNB RTW
From the above, I would want Rows 1 and 3 as they have ABC and MNB that matches.
SELECT RowID FROM MY TABLE
WHERE CONTAINS(Field1, Field2);
I have tried the above, however, this does not work as you cannot specify a 2nd field name in the CONTAINS function.
What am I missing?
You can use like:
select t.*
from t
where field2 like concat('%', field1, '%')
If you want only complete "words" to match -- well, you should fix your data model. You shouldn't be storing lists of things in a string. But, if you must, you can use delimiters:
select t.*
from t
where concat(' ', field2, ' ') like concat('% ', field1, ' %')
I think you're looking for
SELECT *
FROM
(
VALUES
(1, 'ABC', 'ABC DEF'),
(2, 'XYZ', 'WERQ'),
(3, 'MNB', 'MNB RTW'),
-- for col2 like col1
(4, 'HI MNB RTW S', 'MNB RTW')
) T(Id, Col1, Col2)
WHERE CONCAT(' ', Col1, ' ') LIKE CONCAT('% ', Col2, ' %')
OR
CONCAT(' ', Col2, ' ') LIKE CONCAT('% ', Col1, ' %');

sybase sql column concatenation conditionally

Please help me in concatenation the columns,
Col1 Col2 col3
a b c
a3 null null
Expected output is :
col1:a col2:b col3:c
col1:a3
expected is Do not include null column name and value both.
Thanks in advance...
Here is one method:
select stuff( coalesce('col1:' + col1, ' ') +
coalesce('col2:' + col2, ' ') +
coalesce('col3:' + col3, ' '),
1, 1, ''
) as concatted_values

SQL Server: select value, split on a delimiter, then update two columns

I have a SQL Server 2008 database table with three varchar Columns - Col1, Col2, Col3. Col1 has data in it with a single space in between, Col2 and Col3 are empty.
I need to write a query to select the data from Col1, break up each value using the space as the delimiter, and inserting the data on either side of the space into Col2 and Col3 respectively.
I am not too sure how to proceed. Can this be accomplished in SQL, or should I create a small program to do the work for me? I'd appreciate a pointer in the right direction if this can be accomplished via SQL.
Thanks.
UPDATE table SET
Col2 = SUBSTRING(Col1, 1, CHARINDEX(' ', Col1)-1),
Col3 = SUBSTRING(Col1, CHARINDEX(' ', Col1)+1, 8000)
WHERE Col1 LIKE '% %';
If you can guarantee there is only one space:
create table #temp (col1 varchar(50),col2 varchar(50), col3 varchar(50))
insert into #temp (col1)
select 'test 1'
union all
select 'test 2'
union all
select 'test 3'
update #temp
set col2 = left(col1, charindex (' ', col1)),
col3 = substring(col1,charindex (' ', col1)+1, len(col1))
from #temp

concatenate two database columns into one resultset column

I use the following SQL to concatenate several database columns from one table into one column in the result set:
SELECT (field1 + '' + field2 + '' + field3) FROM table1
When one of the fields is null I got null result for the whole concatenation expression. How can I overcome this?
The database is MS SQL Server 2008. By the way, is this the best way to concatenate database columns? Is there any standard SQL doing this?
The SQL standard way of doing this would be:
SELECT COALESCE(field1, '') || COALESCE(field2, '') || COALESCE(field3, '') FROM table1
Example:
INSERT INTO table1 VALUES ('hello', null, 'world');
SELECT COALESCE(field1, '') || COALESCE(field2, '') || COALESCE(field3, '') FROM table1;
helloworld
If you were using SQL 2012 or above you could use the CONCAT function:
SELECT CONCAT(field1, field2, field3) FROM table1
NULL fields won't break your concatenation.
#bummi - Thanks for the comment - edited my answer to correspond to it.
Normal behaviour with NULL is that any operation including a NULL yields a NULL...
- 9 * NULL = NULL
- NULL + '' = NULL
- etc
To overcome this use ISNULL or COALESCE to replace any instances of NULL with something else..
SELECT (ISNULL(field1,'') + '' + ISNULL(field2,'') + '' + ISNULL(field3,'')) FROM table1
If you are having a problem with NULL values, use the COALESCE function to replace the NULL with the value of your choice. Your query would then look like this:
SELECT (COALESCE(field1, '') + '' + COALESCE(field2, '') + '' + COALESCE(field3,'')) FROM table1
http://www.codeproject.com/KB/database/DataCrunching.aspx
Use ISNULL to overcome it.
Example:
SELECT (ISNULL(field1, '') + '' + ISNULL(field2, '')+ '' + ISNULL(field3, '')) FROM table1
This will then replace your NULL content with an empty string which will preserve the concatentation operation from evaluating as an overall NULL result.
If both Column are numeric Then Use This code
Just Cast Column As Varchar(Size)
Example:
Select (Cast(Col1 as Varchar(20)) + '-' + Cast(Col2 as Varchar(20))) As Col3 from Table
Just Cast Column As Varchar(Size)
If both Column are numeric then use code below.
Example:
Select (Cast(Col1 as Varchar(20)) + '-' + Cast(Col2 as Varchar(20))) As Col3 from Table
What will be the size of col3 it will be 40 or something else