title: Formatting SQL Result Within Width of Column
body: <p>I have another question for SQL wizards.</p>
  
  <p>I am using SSMS 2014 and have a query that returns the column titles of a table rolled up into row of a column based off the column field containing a Y or an N. If the column has a Y it should show up in the list and if it contains a N it should be left out of the list.</p>
  
  <p>The content of what is being rolled up is unimportant but it might be important to note that the results in this cell are the concatenation of multiple column titles in another table.</p>
  
  <p>Currently the result looks like this</p>
  
  <p>(From SSMS output)</p>
  
  <p><img src="https://i.stack.imgur.com/K6ViA.png" alt="Results continue but are cut off"></p>
  
  <p>I want the results to look similar this</p>
  
  <p>(screenshot from excel. Image simply shows the result format I am aiming for)</p>
  
  <p><img src="https://i.stack.imgur.com/pdY5I.png" alt="Goal Formatting"></p>
  
  <p>Is there any way of doing this? Could it be possible to display the result this way on a front end website made in Visual Studio 2017? My original web searches kept pointing me to SQL*Plus which I am unfamiliar with and SQLPlus does not appear to work in SSMS.</p>
  
  <p>EDIT: I am not sure how I can make this clearer. I am using SQL in SQL Server Management Studio. I have a query that builds a "commaed"(,) list and puts the result into a single row of a column. The issue is that the list spans beyond the width of the column and the data is replaced with an ellipsis. I want the list to come back stacked so that all the elements in the list appear in the width of the column.</p>
  
  <p>Ultimately the results of this query will be displayed on a web application made in Visual Studio 2017 so if there is a place to treat/format the data on the front end so that when it shows up in a gridview the comma list is stacked up. (Similar to how large numbers can be formatted to have commas when they are displayed on a front end grid view, I want the results in this comma list to be stacked not elongated)</p>
  
  <p>The query code is far to large to post and really has nothing to do with my question.</p>
  
  <p>A final illustration:</p>
  
  <p><em>I want this</em> </p>
  
  <h2>Column Title</h2>
  
  <p>element1,</p>
  
  <p>element2,</p>
  
  <p>element3,</p>
  
  <p><em>Not this</em></p>
  
  <h2>Column Title</h2>
  
  <p>element1,element2,element3,</p>
  
  <p>UPDATE:
  Currently using the following HTML code to display this SQL query on a front end Grid View with enough column width to see the longest data entries. It also takes advantage of the SQL query tacking a ~ on the end of every data piece selected and replacing it with a carriage return. It is better to use a % to set the column width so it scales with different screen sizes (this code uses a pixel specification). </p>
  
  <pre><code>&lt;GridView&gt;
  &lt;Columns&gt;
      &lt;asp:TemplateField HeaderText="Formatted Column Header"&gt;
          &lt;ItemTemplate&gt;
              &lt;div style="width: 400px;"&gt;
                  &lt;%# Eval("SQL Column").ToString().Replace("~","&lt;br /&gt;") %&gt;
               &lt;/div&gt;
          &lt;/ItemTemplate&gt;
      &lt;/asp:TemplateField&gt;
  &lt;/Columns&gt;
  </code></pre>
  
  <p></p>
  
  <p>This approach comes with a three issues. First, This gridview is unbound so the same gridview can be used over to display tables/queries dynamically. Since the gridview is unbound if there is an attempt to display a table that does not contain a SQL column that matches the "SQL column" text specified an error will throw so some conditional HTML (javascript) is required. Second, the original column from the SQL query is shown in the gridview with the ~ still. Third, there is a lot of extra space above and below the template columns data.</p>