Example
col1 col 2 col3
300 Broad ST
,(IsNUll((Cast(FLOOR(col1) as CHAR (7) )),'') + ' ' + IsNull(col2,'') + ' ' + isnull(col3,'')) as col4
result i get is
300 Broad ST
what i want is
300 Broad St.
there is 4 or 5 space between 300 and Broad
the data type for col1 is numeric and for col 2 and 3 is nvarchar. I don't want to change the data type.
This looks a lot like SQL Server. If so:
stuff(coalesce(' ' + Cast(floor(col1) as varchar(7)), '') +
coalesce(' ' + col2, '') +
coalesce(' ' + col3, ''),
1, 1, '') as col4
Related
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
Data
col1 col2 col3
5121 w river road
5512 empty pine road
query 1
Select LTRIM(ISNULL(Col1+' ','')+ ISNULL(col2+' ', '') + ISNULL(col3+' ','') as col4 from ...
query 2
Select LTRIM(COALESCE(Col1+' ','')+ COALESCE(col2+' ', '') + COALESCE(col3+' ','') as col4 from ...
for both results i get this values
5121 w river road ( this looks good )
5512 pine road ( i get extra space for col 2 )
Thank you
The problem is you have three cases
NULL
Length = 0
Length > 0
DEMO
CREATE TABLE Table1
([col1] varchar(5), [col2] varchar(5), [col3] varchar(20))
;
INSERT INTO Table1
([col1], [col2], [col3])
VALUES
('5121', 'w', 'river road'),
('5512', null, 'pine road'),
('3333', '', 'death valley')
;
SELECT COALESCE(CASE WHEN col1 = '' THEN '' ELSE col1 + ' ' END, '') +
COALESCE(CASE WHEN col2 = '' THEN '' ELSE col2 + ' ' END, '') +
COALESCE(CASE WHEN col3 = '' THEN '' ELSE col3 + ' ' END, '')
FROM Table1
OUTPUT
Original OP and Jhon version work OK with NULL and length > 0 but fail with length = 0
Consider Concat() if 2012+
As you can see with col2 ... null+' ' will result in a null, the good news is concat() represents nulls as ''
Declare #YourTable table (col1 varchar(50),col2 varchar(50),col3 varchar(50))
Insert Into #YourTable values
('5121','w', 'river road'),
('5512','','pine road'),
('1313',null,'mocking bird lane')
Select concat(nullif(col1,'')+' ',nullif(col2,'')+' ',nullif(col3,''))
From #YourTable
Returns
5121 w river road
5512 pine road -- No Extra Space
1313 mocking bird lane -- NULLs handled
this is my Data
col1 col2 col col3 col4 col5 col6
42 A 11 18 89 16 empty
42 B 12 empty 89 14 C
36 8 9 empty empty 2 empty
this is the Script I'm running
select col1 + COALESCE ([col2]+'-','')
+COALESCE([col3]+'-','')+COALESCE([col4]+'-','')
+COALESCE([col5]+'-','')+COALESCE([col6],'') as totalCol
FROM ...
This is what I get
totalCol
42A-11-18-89-16-
42B-12- -89-14-C
368-9- - -2 -
This is what I want
totalCol
42A-11-18-89-16
42B-12-89-14-C
368-9-2
run a replace of "- " to "" after your script
also you have to remember that nulls and blank space are two different things.
Updated script
select ltrim(rtrim(replace(replace(
col1 + COALESCE ([col2]+'-','')
+COALESCE([col3]+'-','')+COALESCE([col4]+'-','')
+COALESCE([col5]+'-','')+COALESCE([col6],'') + ' ','--','-'),
'- ','')))
as totalCol
FROM ...
or assuming only blank space and not nulls
select ltrim(rtrim(replace(
[col1] + [col2]+'-' + [col3]+'-' + [col4]+'-' + [col5]+'-' + [col6] + ' '
'- ','')))
as totalCol
FROM ...
Assuming you have contrived the data to illustrate the issue (because you have col in your data, but don't use col in your query), you can easily do it this way. A dash (-) is added after all non-null data. Then, we pull all but the last character (since it will always be a dash).
I also added more code to create the data, so that you can copy/run the whole query and see that it works.
declare #tbl table (
col1 varchar(10) null,
col2 varchar(10) null,
col3 varchar(10) null,
col4 varchar(10) null,
col5 varchar(10) null,
col6 varchar(10) null,
col7 varchar(10) null
)
insert into #tbl values
('42', 'A', '11', '18', '89', '16', null),
('42', 'B', '12', null, '89', '14', 'C'),
('36', '8', '9', null, null, '2', null)
select left(x.concat_val, len(x.concat_val) - 1) as totalCol
from (
select isnull(col1 + '-', '') +
isnull(col2 + '-', '') +
isnull(col3 + '-', '') +
isnull(col4 + '-', '') +
isnull(col5 + '-', '') +
isnull(col6 + '-', '') +
isnull(col7 + '-', '') as concat_val
from #tbl
) as x
Here's the returned data:
totalCol
-----------------------------------------------------------------------------
42-A-11-18-89-16
42-B-12-89-14-C
36-8-9-2
I have a table CommentsTable with columns like, CommentA, CommentB, CommentC, CommentD, CommentE.
All comments columns are VARCHAR (200), by default all columns are NULL also.
The data looks like:
CommentId CommentA CommentB CommentC CommentD CommentE
---------------------------------------------------------------------
12345 NULL C 001 C 002 NULL C 003
45678 C 005 NULL NULL C 007 NULL
67890 C 010 NULL C 011 C 012 NULL
36912 C 021 C 023 C 024 C 025 C 026
I need to avoid the null values and the remaining values are concatenate with comma.
So, the expected output like:
CommentId CommetDetails
-------------------------------
12345 C 001, C 002, C 003
45678 C 005, C 007
67890 C 010, C 011, C 012
36912 C 021, C 023, C 024, C 025, C 026
I tried with simple query:
SELECT CommentId, ISNULL(CommentA, '') + ', ' + ISNULL(CommentB, '') + ', ' +
ISNULL(CommentC, '') + ', ' + ISNULL(CommentD, '') + ', ' +
ISNULL(CommentE, '') [CommentDetails]
FROM CommentsTable
WHERE ...... --Some conditions
But the unwanted comma are occurred, So added IIF
SELECT CommentId,
IIF(ISNULL(CommentA, '') <> '', (CommentA + ', '), '') +
IIF(ISNULL(CommentB, '') <> '', (CommentB + ', '), '') +
IIF(ISNULL(CommentC, '') <> '', (CommentC + ', '), '') +
IIF(ISNULL(CommentD, '') <> '', (CommentD + ', '), '') +
ISNULL(CommentE, '') [CommentDetails]
FROM CommentsTable
WHERE ...... --Some conditions
But here also, the comma occurred in the last position for some cases (If CommentD, CommetE are NULL.
Is there any way to achieve to solve for all the cases.
Sample SQL Fiddle
You can use ISNULL like this ISNULL(',' + CommentA, '') and write your query like this.
SELECT CommentId,
STUFF(
ISNULL(',' + CommentA, '') +
ISNULL(',' + CommentB, '') +
ISNULL(',' + CommentC, '') +
ISNULL(',' + CommentD, '') +
ISNULL(',' + CommentE, ''),1,1,'') as [CommentDetails]
FROM CommentsTable
WHERE ...... //Some conditions
See result in SQL Fiddle.
The above answers are correct and no challenge to the accepted answer but in case some columns have empty string instead of null then below might help. Please don't hesitate for a better approach and correct me if it's wrong.
SELECT CommentId,
STUFF(
ISNULL(',' + CASE WHEN CommentA= '' THEN NULL ELSE CommentA END, '') +
ISNULL(',' + CASE WHEN CommentB= '' THEN NULL ELSE CommentB END, '') +
ISNULL(',' + CASE WHEN CommentC= '' THEN NULL ELSE CommentC END, '') +
ISNULL(',' + CASE WHEN CommentD= '' THEN NULL ELSE CommentD END, '') +
ISNULL(',' + CASE WHEN CommentE= '' THEN NULL ELSE CommentE END, ''),1,1,'') as [CommentDetails]
FROM CommentsTable
create table #test
(
CommentId int,
CommentA nvarchar(200),
CommentB nvarchar(200),
CommentC nvarchar(200),
CommentD nvarchar(200),
CommentE nvarchar(200)
)
insert into #test values(12345,NULL,'C 001','C 002',NULL,'C 003')
insert into #test values(45678,'C 005',NULL,NULL,'C 007',NULL)
insert into #test values(67890,'C 010',NULL,'C 011','C 012',NULL)
insert into #test values(36912,'C 021','C 023','C 024','C 025','C 026')
Use This Code:
select CommentId,STUFF(ISNULL(','+CommentA,'')+
ISNULL(','+CommentB,'')+
ISNULL(','+CommentC,'')+
ISNULL(','+CommentD,'')+
ISNULL(','+CommentE,''),1,1,'') As Comment
from #test
order by CommentId
I have a 2 columns in a SQL Server table.
One is characters like 'Attn: firstname lastname', and the other column has a number associated with that person.
I can't figure out how to query them so the info comes out as:
Lastname, Firstname - number
If you are in SQL Server you can do this
Select Col1 + ' - ' + Col2
From dbo.{Table}
Where {Conditions}
If you mean you need to drop the "attn:" you can do something like this
Select substring(Col1, 5,len(Col1)-5) + ' - ' + Col2
From dbo.{Table}
Where {Conditions}
Instead of hardcoding the index of ':', you can do something like this
SELECT REPLACE(RIGHT(Col1, (LEN(Col1) - CHARINDEX(':', Col1)-1)),' ',', ') + ' - ' + Col2
To test this you can run the following
DECLARE #column1string AS VARCHAR(30), #column2number AS VARCHAR(10)
SET #column1string = 'Attn: Firstname Lastname'
SET #column2number = '12345678'
SELECT REPLACE(RIGHT(#column1string, (LEN(#column1string) - CHARINDEX(':', #column1string)-1)),' ',', ') + ' - ' + #column2number