Get the Count of Concatenating Column in sql











up vote
1
down vote

favorite
2












i have Concatenating the multiple Columns and i want the count of how many columns are Concatenated



the query output and expected output are



for Concatenation



select  ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '')


table



===================================
| col1 | col2 | col3 | col4 |
===================================
| 1 | 2 | NULL | NULL |
| NULL | NULL | NULL | NULL |
| 1 | NULL | NULL | NULL |
| NULL | 2 | 3 | 4 |
| | NULL | NULL | NULL |
==================================


the resulted output



 ==============
|ConcateColumn|
==============
|1*2 |
|NULL |
|1 |
|2*3*4 |
| |
===============


Expected Output



------------------------
| Count | ConcateColumn |
-------------------------
| 2 | 1*2 |
| 0 | NULL |
| 1 | 1 |
| 3 | 2*3*4 |
| 0 | |
-------------------------


if i get the count that is more than enough










share|improve this question






















  • Off the top of my head, you could do something using CASE statements and a subquery to achieve this. What have you already tried to calculate this?
    – Martin Parkin
    Nov 11 at 10:59










  • please note that the no of columns are dynamic
    – Ramapriyan C
    Nov 11 at 10:59















up vote
1
down vote

favorite
2












i have Concatenating the multiple Columns and i want the count of how many columns are Concatenated



the query output and expected output are



for Concatenation



select  ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '')


table



===================================
| col1 | col2 | col3 | col4 |
===================================
| 1 | 2 | NULL | NULL |
| NULL | NULL | NULL | NULL |
| 1 | NULL | NULL | NULL |
| NULL | 2 | 3 | 4 |
| | NULL | NULL | NULL |
==================================


the resulted output



 ==============
|ConcateColumn|
==============
|1*2 |
|NULL |
|1 |
|2*3*4 |
| |
===============


Expected Output



------------------------
| Count | ConcateColumn |
-------------------------
| 2 | 1*2 |
| 0 | NULL |
| 1 | 1 |
| 3 | 2*3*4 |
| 0 | |
-------------------------


if i get the count that is more than enough










share|improve this question






















  • Off the top of my head, you could do something using CASE statements and a subquery to achieve this. What have you already tried to calculate this?
    – Martin Parkin
    Nov 11 at 10:59










  • please note that the no of columns are dynamic
    – Ramapriyan C
    Nov 11 at 10:59













up vote
1
down vote

favorite
2









up vote
1
down vote

favorite
2






2





i have Concatenating the multiple Columns and i want the count of how many columns are Concatenated



the query output and expected output are



for Concatenation



select  ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '')


table



===================================
| col1 | col2 | col3 | col4 |
===================================
| 1 | 2 | NULL | NULL |
| NULL | NULL | NULL | NULL |
| 1 | NULL | NULL | NULL |
| NULL | 2 | 3 | 4 |
| | NULL | NULL | NULL |
==================================


the resulted output



 ==============
|ConcateColumn|
==============
|1*2 |
|NULL |
|1 |
|2*3*4 |
| |
===============


Expected Output



------------------------
| Count | ConcateColumn |
-------------------------
| 2 | 1*2 |
| 0 | NULL |
| 1 | 1 |
| 3 | 2*3*4 |
| 0 | |
-------------------------


if i get the count that is more than enough










share|improve this question













i have Concatenating the multiple Columns and i want the count of how many columns are Concatenated



the query output and expected output are



for Concatenation



select  ConcateColumn = STUFF(
COALESCE('* ' + RTRIM(col1),'')
+COALESCE('* ' + RTRIM(col2),'')
+COALESCE('* ' + RTRIM(col4),'')
+COALESCE('* ' + RTRIM(col3),'')
, 1, 2, '')


table



===================================
| col1 | col2 | col3 | col4 |
===================================
| 1 | 2 | NULL | NULL |
| NULL | NULL | NULL | NULL |
| 1 | NULL | NULL | NULL |
| NULL | 2 | 3 | 4 |
| | NULL | NULL | NULL |
==================================


the resulted output



 ==============
|ConcateColumn|
==============
|1*2 |
|NULL |
|1 |
|2*3*4 |
| |
===============


Expected Output



------------------------
| Count | ConcateColumn |
-------------------------
| 2 | 1*2 |
| 0 | NULL |
| 1 | 1 |
| 3 | 2*3*4 |
| 0 | |
-------------------------


if i get the count that is more than enough







c# sql .net sql-server model-view-controller






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 10:53









Ramapriyan C

206




206












  • Off the top of my head, you could do something using CASE statements and a subquery to achieve this. What have you already tried to calculate this?
    – Martin Parkin
    Nov 11 at 10:59










  • please note that the no of columns are dynamic
    – Ramapriyan C
    Nov 11 at 10:59


















  • Off the top of my head, you could do something using CASE statements and a subquery to achieve this. What have you already tried to calculate this?
    – Martin Parkin
    Nov 11 at 10:59










  • please note that the no of columns are dynamic
    – Ramapriyan C
    Nov 11 at 10:59
















Off the top of my head, you could do something using CASE statements and a subquery to achieve this. What have you already tried to calculate this?
– Martin Parkin
Nov 11 at 10:59




Off the top of my head, you could do something using CASE statements and a subquery to achieve this. What have you already tried to calculate this?
– Martin Parkin
Nov 11 at 10:59












please note that the no of columns are dynamic
– Ramapriyan C
Nov 11 at 10:59




please note that the no of columns are dynamic
– Ramapriyan C
Nov 11 at 10:59












4 Answers
4






active

oldest

votes

















up vote
1
down vote



accepted










You can count the number of * in the concatenated string:



with cte as 
(
select
ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T
)
select ConcateColumn,
-- how many '*' have been removed?
coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
from cte
;


Of course, this will return wrong numbers if your data contains *






share|improve this answer























  • if the column is empty i need count as 0 , but im getting as 1
    – Ramapriyan C
    Nov 11 at 11:18










  • What do you mean by empty? Empty string ''? It works with the D-Shih's dbfiddle.uk/…
    – dnoeth
    Nov 11 at 11:22










  • yes '' will return count as 1
    – Ramapriyan C
    Nov 11 at 11:23










  • check it in fiddle in last insert column
    – Ramapriyan C
    Nov 11 at 11:23






  • 1




    Well, an empty string is not the same as a NULL, of course it's counted.
    – dnoeth
    Nov 11 at 11:24


















up vote
2
down vote













You can try this.



select  (SELECT count(NULLIF(val,''))
FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
ConcateColumn = STUFF(
COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
+COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
, 1, 2, '')
FROM T


If you want to count without NULL and '' you can try to use NULLIF function.



sqlfiddle






share|improve this answer



















  • 1




    count instead of sum, but you beat me to it... +1 :-)
    – Zohar Peled
    Nov 11 at 11:01












  • @ZoharPeled Yes Thanks
    – D-Shih
    Nov 11 at 11:02










  • please note that the columns may a string, just for understanding i gave it as numbers
    – Ramapriyan C
    Nov 11 at 11:04










  • if i give string it is not working pls check
    – Ramapriyan C
    Nov 11 at 11:08






  • 1




    If you want empty strings treated as NULLs, you must add NULLIF, e.g. `RTRIM(NULLIF(col1),''))
    – dnoeth
    Nov 11 at 11:27


















up vote
1
down vote













In SQL Server 2017+, you can simply do:



select v.*
from t cross apply
(select count(*) as cnt,
string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
from (values (1, col1), (2, col2), (3, col4), (4, col3)
) v(col, ord)
where col is not null
) v;


In earlier versions, I would probably follow the same structure that you have:



select ( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end)
) as cnt





share|improve this answer




























    up vote
    0
    down vote













     select sum(
    case when col1 = '' then 0 when col1 is null then 0 else 1 end +
    case when col2 = '' then 0 when col2 is null then 0 else 1 end +
    case when col3 = '' then 0 when col3 is null then 0 else 1 end +
    case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
    ConcateColumn = STUFF(
    COALESCE('* ' + RTRIM(col1),'')
    +COALESCE('* ' + RTRIM(col2),'')
    +COALESCE('* ' + RTRIM(col4),'')
    +COALESCE('* ' + RTRIM(col3),'')
    , 1, 2, '') from temp1
    group by col1,col2,col3,col4





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248008%2fget-the-count-of-concatenating-column-in-sql%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      You can count the number of * in the concatenated string:



      with cte as 
      (
      select
      ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
      , 1, 2, '')
      FROM T
      )
      select ConcateColumn,
      -- how many '*' have been removed?
      coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
      from cte
      ;


      Of course, this will return wrong numbers if your data contains *






      share|improve this answer























      • if the column is empty i need count as 0 , but im getting as 1
        – Ramapriyan C
        Nov 11 at 11:18










      • What do you mean by empty? Empty string ''? It works with the D-Shih's dbfiddle.uk/…
        – dnoeth
        Nov 11 at 11:22










      • yes '' will return count as 1
        – Ramapriyan C
        Nov 11 at 11:23










      • check it in fiddle in last insert column
        – Ramapriyan C
        Nov 11 at 11:23






      • 1




        Well, an empty string is not the same as a NULL, of course it's counted.
        – dnoeth
        Nov 11 at 11:24















      up vote
      1
      down vote



      accepted










      You can count the number of * in the concatenated string:



      with cte as 
      (
      select
      ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
      , 1, 2, '')
      FROM T
      )
      select ConcateColumn,
      -- how many '*' have been removed?
      coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
      from cte
      ;


      Of course, this will return wrong numbers if your data contains *






      share|improve this answer























      • if the column is empty i need count as 0 , but im getting as 1
        – Ramapriyan C
        Nov 11 at 11:18










      • What do you mean by empty? Empty string ''? It works with the D-Shih's dbfiddle.uk/…
        – dnoeth
        Nov 11 at 11:22










      • yes '' will return count as 1
        – Ramapriyan C
        Nov 11 at 11:23










      • check it in fiddle in last insert column
        – Ramapriyan C
        Nov 11 at 11:23






      • 1




        Well, an empty string is not the same as a NULL, of course it's counted.
        – dnoeth
        Nov 11 at 11:24













      up vote
      1
      down vote



      accepted







      up vote
      1
      down vote



      accepted






      You can count the number of * in the concatenated string:



      with cte as 
      (
      select
      ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
      , 1, 2, '')
      FROM T
      )
      select ConcateColumn,
      -- how many '*' have been removed?
      coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
      from cte
      ;


      Of course, this will return wrong numbers if your data contains *






      share|improve this answer














      You can count the number of * in the concatenated string:



      with cte as 
      (
      select
      ConcateColumn = STUFF( COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
      , 1, 2, '')
      FROM T
      )
      select ConcateColumn,
      -- how many '*' have been removed?
      coalesce(len(ConcateColumn) - len(replace(ConcateColumn, '*', '')) + 1, 0)
      from cte
      ;


      Of course, this will return wrong numbers if your data contains *







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 11 at 11:29

























      answered Nov 11 at 11:11









      dnoeth

      44k31838




      44k31838












      • if the column is empty i need count as 0 , but im getting as 1
        – Ramapriyan C
        Nov 11 at 11:18










      • What do you mean by empty? Empty string ''? It works with the D-Shih's dbfiddle.uk/…
        – dnoeth
        Nov 11 at 11:22










      • yes '' will return count as 1
        – Ramapriyan C
        Nov 11 at 11:23










      • check it in fiddle in last insert column
        – Ramapriyan C
        Nov 11 at 11:23






      • 1




        Well, an empty string is not the same as a NULL, of course it's counted.
        – dnoeth
        Nov 11 at 11:24


















      • if the column is empty i need count as 0 , but im getting as 1
        – Ramapriyan C
        Nov 11 at 11:18










      • What do you mean by empty? Empty string ''? It works with the D-Shih's dbfiddle.uk/…
        – dnoeth
        Nov 11 at 11:22










      • yes '' will return count as 1
        – Ramapriyan C
        Nov 11 at 11:23










      • check it in fiddle in last insert column
        – Ramapriyan C
        Nov 11 at 11:23






      • 1




        Well, an empty string is not the same as a NULL, of course it's counted.
        – dnoeth
        Nov 11 at 11:24
















      if the column is empty i need count as 0 , but im getting as 1
      – Ramapriyan C
      Nov 11 at 11:18




      if the column is empty i need count as 0 , but im getting as 1
      – Ramapriyan C
      Nov 11 at 11:18












      What do you mean by empty? Empty string ''? It works with the D-Shih's dbfiddle.uk/…
      – dnoeth
      Nov 11 at 11:22




      What do you mean by empty? Empty string ''? It works with the D-Shih's dbfiddle.uk/…
      – dnoeth
      Nov 11 at 11:22












      yes '' will return count as 1
      – Ramapriyan C
      Nov 11 at 11:23




      yes '' will return count as 1
      – Ramapriyan C
      Nov 11 at 11:23












      check it in fiddle in last insert column
      – Ramapriyan C
      Nov 11 at 11:23




      check it in fiddle in last insert column
      – Ramapriyan C
      Nov 11 at 11:23




      1




      1




      Well, an empty string is not the same as a NULL, of course it's counted.
      – dnoeth
      Nov 11 at 11:24




      Well, an empty string is not the same as a NULL, of course it's counted.
      – dnoeth
      Nov 11 at 11:24












      up vote
      2
      down vote













      You can try this.



      select  (SELECT count(NULLIF(val,''))
      FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
      ConcateColumn = STUFF(
      COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
      , 1, 2, '')
      FROM T


      If you want to count without NULL and '' you can try to use NULLIF function.



      sqlfiddle






      share|improve this answer



















      • 1




        count instead of sum, but you beat me to it... +1 :-)
        – Zohar Peled
        Nov 11 at 11:01












      • @ZoharPeled Yes Thanks
        – D-Shih
        Nov 11 at 11:02










      • please note that the columns may a string, just for understanding i gave it as numbers
        – Ramapriyan C
        Nov 11 at 11:04










      • if i give string it is not working pls check
        – Ramapriyan C
        Nov 11 at 11:08






      • 1




        If you want empty strings treated as NULLs, you must add NULLIF, e.g. `RTRIM(NULLIF(col1),''))
        – dnoeth
        Nov 11 at 11:27















      up vote
      2
      down vote













      You can try this.



      select  (SELECT count(NULLIF(val,''))
      FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
      ConcateColumn = STUFF(
      COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
      , 1, 2, '')
      FROM T


      If you want to count without NULL and '' you can try to use NULLIF function.



      sqlfiddle






      share|improve this answer



















      • 1




        count instead of sum, but you beat me to it... +1 :-)
        – Zohar Peled
        Nov 11 at 11:01












      • @ZoharPeled Yes Thanks
        – D-Shih
        Nov 11 at 11:02










      • please note that the columns may a string, just for understanding i gave it as numbers
        – Ramapriyan C
        Nov 11 at 11:04










      • if i give string it is not working pls check
        – Ramapriyan C
        Nov 11 at 11:08






      • 1




        If you want empty strings treated as NULLs, you must add NULLIF, e.g. `RTRIM(NULLIF(col1),''))
        – dnoeth
        Nov 11 at 11:27













      up vote
      2
      down vote










      up vote
      2
      down vote









      You can try this.



      select  (SELECT count(NULLIF(val,''))
      FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
      ConcateColumn = STUFF(
      COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
      , 1, 2, '')
      FROM T


      If you want to count without NULL and '' you can try to use NULLIF function.



      sqlfiddle






      share|improve this answer














      You can try this.



      select  (SELECT count(NULLIF(val,''))
      FROM (VALUES(col1),(col2),(col3), (col4)) v (val)) 'COUNT',
      ConcateColumn = STUFF(
      COALESCE('* ' + NULLIF(RTRIM(col1),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col2),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col3),''),'')
      +COALESCE('* ' + NULLIF(RTRIM(col4),''),'')
      , 1, 2, '')
      FROM T


      If you want to count without NULL and '' you can try to use NULLIF function.



      sqlfiddle







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 11 at 11:33

























      answered Nov 11 at 11:00









      D-Shih

      24.4k61431




      24.4k61431








      • 1




        count instead of sum, but you beat me to it... +1 :-)
        – Zohar Peled
        Nov 11 at 11:01












      • @ZoharPeled Yes Thanks
        – D-Shih
        Nov 11 at 11:02










      • please note that the columns may a string, just for understanding i gave it as numbers
        – Ramapriyan C
        Nov 11 at 11:04










      • if i give string it is not working pls check
        – Ramapriyan C
        Nov 11 at 11:08






      • 1




        If you want empty strings treated as NULLs, you must add NULLIF, e.g. `RTRIM(NULLIF(col1),''))
        – dnoeth
        Nov 11 at 11:27














      • 1




        count instead of sum, but you beat me to it... +1 :-)
        – Zohar Peled
        Nov 11 at 11:01












      • @ZoharPeled Yes Thanks
        – D-Shih
        Nov 11 at 11:02










      • please note that the columns may a string, just for understanding i gave it as numbers
        – Ramapriyan C
        Nov 11 at 11:04










      • if i give string it is not working pls check
        – Ramapriyan C
        Nov 11 at 11:08






      • 1




        If you want empty strings treated as NULLs, you must add NULLIF, e.g. `RTRIM(NULLIF(col1),''))
        – dnoeth
        Nov 11 at 11:27








      1




      1




      count instead of sum, but you beat me to it... +1 :-)
      – Zohar Peled
      Nov 11 at 11:01






      count instead of sum, but you beat me to it... +1 :-)
      – Zohar Peled
      Nov 11 at 11:01














      @ZoharPeled Yes Thanks
      – D-Shih
      Nov 11 at 11:02




      @ZoharPeled Yes Thanks
      – D-Shih
      Nov 11 at 11:02












      please note that the columns may a string, just for understanding i gave it as numbers
      – Ramapriyan C
      Nov 11 at 11:04




      please note that the columns may a string, just for understanding i gave it as numbers
      – Ramapriyan C
      Nov 11 at 11:04












      if i give string it is not working pls check
      – Ramapriyan C
      Nov 11 at 11:08




      if i give string it is not working pls check
      – Ramapriyan C
      Nov 11 at 11:08




      1




      1




      If you want empty strings treated as NULLs, you must add NULLIF, e.g. `RTRIM(NULLIF(col1),''))
      – dnoeth
      Nov 11 at 11:27




      If you want empty strings treated as NULLs, you must add NULLIF, e.g. `RTRIM(NULLIF(col1),''))
      – dnoeth
      Nov 11 at 11:27










      up vote
      1
      down vote













      In SQL Server 2017+, you can simply do:



      select v.*
      from t cross apply
      (select count(*) as cnt,
      string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
      from (values (1, col1), (2, col2), (3, col4), (4, col3)
      ) v(col, ord)
      where col is not null
      ) v;


      In earlier versions, I would probably follow the same structure that you have:



      select ( (case when col1 is not null then 1 else 0 end) +
      (case when col2 is not null then 1 else 0 end) +
      (case when col4 is not null then 1 else 0 end) +
      (case when col3 is not null then 1 else 0 end)
      ) as cnt





      share|improve this answer

























        up vote
        1
        down vote













        In SQL Server 2017+, you can simply do:



        select v.*
        from t cross apply
        (select count(*) as cnt,
        string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
        from (values (1, col1), (2, col2), (3, col4), (4, col3)
        ) v(col, ord)
        where col is not null
        ) v;


        In earlier versions, I would probably follow the same structure that you have:



        select ( (case when col1 is not null then 1 else 0 end) +
        (case when col2 is not null then 1 else 0 end) +
        (case when col4 is not null then 1 else 0 end) +
        (case when col3 is not null then 1 else 0 end)
        ) as cnt





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          In SQL Server 2017+, you can simply do:



          select v.*
          from t cross apply
          (select count(*) as cnt,
          string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
          from (values (1, col1), (2, col2), (3, col4), (4, col3)
          ) v(col, ord)
          where col is not null
          ) v;


          In earlier versions, I would probably follow the same structure that you have:



          select ( (case when col1 is not null then 1 else 0 end) +
          (case when col2 is not null then 1 else 0 end) +
          (case when col4 is not null then 1 else 0 end) +
          (case when col3 is not null then 1 else 0 end)
          ) as cnt





          share|improve this answer












          In SQL Server 2017+, you can simply do:



          select v.*
          from t cross apply
          (select count(*) as cnt,
          string_agg(rtrim(col), '* ') within group (order by ord) as ConcateColumn
          from (values (1, col1), (2, col2), (3, col4), (4, col3)
          ) v(col, ord)
          where col is not null
          ) v;


          In earlier versions, I would probably follow the same structure that you have:



          select ( (case when col1 is not null then 1 else 0 end) +
          (case when col2 is not null then 1 else 0 end) +
          (case when col4 is not null then 1 else 0 end) +
          (case when col3 is not null then 1 else 0 end)
          ) as cnt






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 11:36









          Gordon Linoff

          748k34285391




          748k34285391






















              up vote
              0
              down vote













               select sum(
              case when col1 = '' then 0 when col1 is null then 0 else 1 end +
              case when col2 = '' then 0 when col2 is null then 0 else 1 end +
              case when col3 = '' then 0 when col3 is null then 0 else 1 end +
              case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
              ConcateColumn = STUFF(
              COALESCE('* ' + RTRIM(col1),'')
              +COALESCE('* ' + RTRIM(col2),'')
              +COALESCE('* ' + RTRIM(col4),'')
              +COALESCE('* ' + RTRIM(col3),'')
              , 1, 2, '') from temp1
              group by col1,col2,col3,col4





              share|improve this answer

























                up vote
                0
                down vote













                 select sum(
                case when col1 = '' then 0 when col1 is null then 0 else 1 end +
                case when col2 = '' then 0 when col2 is null then 0 else 1 end +
                case when col3 = '' then 0 when col3 is null then 0 else 1 end +
                case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
                ConcateColumn = STUFF(
                COALESCE('* ' + RTRIM(col1),'')
                +COALESCE('* ' + RTRIM(col2),'')
                +COALESCE('* ' + RTRIM(col4),'')
                +COALESCE('* ' + RTRIM(col3),'')
                , 1, 2, '') from temp1
                group by col1,col2,col3,col4





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                   select sum(
                  case when col1 = '' then 0 when col1 is null then 0 else 1 end +
                  case when col2 = '' then 0 when col2 is null then 0 else 1 end +
                  case when col3 = '' then 0 when col3 is null then 0 else 1 end +
                  case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
                  ConcateColumn = STUFF(
                  COALESCE('* ' + RTRIM(col1),'')
                  +COALESCE('* ' + RTRIM(col2),'')
                  +COALESCE('* ' + RTRIM(col4),'')
                  +COALESCE('* ' + RTRIM(col3),'')
                  , 1, 2, '') from temp1
                  group by col1,col2,col3,col4





                  share|improve this answer












                   select sum(
                  case when col1 = '' then 0 when col1 is null then 0 else 1 end +
                  case when col2 = '' then 0 when col2 is null then 0 else 1 end +
                  case when col3 = '' then 0 when col3 is null then 0 else 1 end +
                  case when col4 = '' then 0 when col4 is null then 0 else 1 end ) 'COUNT',
                  ConcateColumn = STUFF(
                  COALESCE('* ' + RTRIM(col1),'')
                  +COALESCE('* ' + RTRIM(col2),'')
                  +COALESCE('* ' + RTRIM(col4),'')
                  +COALESCE('* ' + RTRIM(col3),'')
                  , 1, 2, '') from temp1
                  group by col1,col2,col3,col4






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 12:28









                  Hitham Yosri

                  11




                  11






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248008%2fget-the-count-of-concatenating-column-in-sql%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine