-6

I am trying to find out is there a way to take the string data in the Name, Address, and DSL columns that has been concatenated in STUFF() and XML PATH and display it vertically in same row instead of horizontally on the front end. I am using a gridview to display the information.

  SELECT  i.[InstrumentID], it.[InstrumentType],i.[InstrumentNumber], i.[NANumber],i.[DateTimeFiled],
    STUFF((SELECT ',   ' + n.[Surname] + n.[GivenName] + '(' + nc.[NameCode] + ')'  FROM [dbo].[tblName] n 
INNER JOIN [dbo].[tblNameCode] nc ON nc.[NameCodeID] = n.[NameCodeID] Where n.[InstrumentID] = i.[InstrumentID] ORDER BY  n.[Surname] FOR XML PATH (''), TYPE).value('.', 'varchar(max)'), 1, 1, '') AS Name ,
    STUFF(( SELECT ',' + a.[StreetNumber] + a.[StreetName] FROM [dbo].[tblAddress] a Where a.[InstrumentID] = i.[InstrumentID] ORDER BY  a.[StreetName] FOR XML PATH (''), TYPE).value('.', 'varchar(max)'), 1, 1, '') AS Address,
    STUFF(( SELECT ''  + d.[District] + ' ' +d.[Square]+ ' ' + d.[Lot] + ', ' FROM [dbo].[tblDLS] d WHERE d.[InstrumentID] = i.[InstrumentID] FOR XML PATH (''), TYPE).value('.', 'varchar(max)'), 1, 1,'') AS DSL
FROM [dbo].[tblInstrument] i 
INNER JOIN [dbo].[tblInstrumentType] it ON i.[InstrumentTypeID] = it.[InstrumentTypeID] 
WHERE InstrumentNumber = 1
ORDER BY InstrumentNumber, InstrumentType 

Here is my output

SampleTable

There is nothing wrong as far as getting results. The issue is getting the data Vertical. Here is a screenshot of my program also:

SamplePage

Here is the aspx code for displaying the info:

<Columns>
 <asp:TemplateField HeaderText="Instrument #, Type, Dist., Squ. , Lot">
                       <ItemTemplate>
                            <asp:LinkButton ID="LbPath" runat="server" 
                               Text='<%# String.Format("{0} {1}", Eval("InstrumentNumber"), Eval("InstrumentType")) %>' 
                                CommandName="GetInstrument" 
                                CommandArgument='<%#Bind("instrumentID") %>'>
                            </asp:LinkButton>
                           <br />
                           <asp:Label ID="lblDateFiled" runat="server" Text='<%# Eval("DateTimeFiled") %>'> </asp:Label>
                             <br />
                            <asp:Label ID="lblNANumber" runat="server" Font-Names="Segoe UI Semibold" Font-Size="Medium" Text="NA Number: " ForeColor="#434343"></asp:Label><%# Eval("NANumber") %>
                           <br />
                           <br />
                           <asp:Label ID="lblDSL" runat="server" Text='<%#Eval("DSL") %>'> </asp:Label> 
                            <%--<asp:Label ID="lblDSL" runat="server" Text='<%# String.Format("{0} {1} {2}", Eval("District"), Eval("Square"),  Eval("Lot")) %>'> </asp:Label> --%>                           
                        </ItemTemplate>
                   </asp:TemplateField>
                        <asp:TemplateField HeaderText="Name, Address" SortExpression="Surname">
                <ItemTemplate>
                    <EditItemTemplate>
                        
                   <%-- <asp:Label ID="label1"  runat="server" Text='<%# Bind("instrumentID") %>' ></asp:Label>--%>
                </EditItemTemplate>
                                    <asp:Label ID="lblName" runat="server" Text='<%#  Eval("Name") %>'></asp:Label>
<%--                    <asp:Label ID="lblName" runat="server" Text='<%# String.Format(" {0}, {1} ({2})", Eval("Surname"), Eval("GivenName"), Eval("Namecode")) %>'></asp:Label>--%>
                    <br />
                    <asp:Label ID="lblAddress" runat="server" Font-Names="Segoe UI Semibold" Font-Size="Medium" Text='<%# Eval("Address") %>'></asp:Label>
                   <%--<asp:Label ID="lblAddress" runat="server" Font-Names="Segoe UI Semibold" Font-Size="Medium" Text='<%# String.Format("{0} {1} {2}", Eval("Subdivision"), Eval("StreetNumber"), Eval("StreetName")) %>'></asp:Label>--%>
                </ItemTemplate>
            </asp:TemplateField>
                  <%-- <asp:BoundField DataField="InstrumentType" HeaderText="Instrument Type" InsertVisible="false" ReadOnly="true" SortExpression="InstrumentType" />
                   <asp:BoundField DataField="NANumber" HeaderText="NANumber" InsertVisible="false" SortExpression="NANumber" />
                   <asp:BoundField DataField="DateTimeFiled" HeaderText="DateTimeFiled" InsertVisible="false" ReadOnly="true" SortExpression="DateTimeFiled" />--%>

               </Columns>
BL-OOP
  • 9
  • 5
  • 4
    I don't get it. What does how you display the data got to do with Sql Server? – stuartd Sep 30 '20 at 21:48
  • 2
    This feels more like a Presentation Layer request. Even then, it is not clear what you mean vertically. Are you looking for Name & Address to be vertical in one cell? If so, and assuming HTML, just add
    when you construct the string. Furthermore, since you are on 2017 you can use string_agg() instead of the stuff/xml method.
    – John Cappelletti Sep 30 '20 at 22:11
  • @JohnCappelletti yes you could say presentation layer. I edited my question and added more clarity to see if that helps. I am trying to just get the data to display stacked vertically in one column instead of inline on one row with delimiter. Thanks for taking the time out – BL-OOP Oct 01 '20 at 14:03

1 Answers1

1

Assuming I understand your need, you could try something like the following:

/* Create a mock-up table with sample data */

DECLARE @Data TABLE (
    InstrumentID INT, InstrumentType VARCHAR(50), InstrumentNumber INT, NANumber INT, DateTimeFiled DATETIME, [Name] VARCHAR(255) 
);

INSERT INTO @Data ( InstrumentID, InstrumentType, InstrumentNumber, NANumber, DateTimeFiled, [Name] )
VALUES
    ( 1625168, 'ACCOUNTS RECEIVABLE', 1, 785401, '1989-01-03 09:48:03.000', 'HIBERNIA NATIONAL BANK NEW ORLEANS' ),
    ( 1382385, 'SALE', 1, 785467, '1989-01-03 10:14:03.000', 'M & M RESTAURANT AND LOUNGE, INC' );

/* "Pivot" the columnar data */
SELECT
    x.f.value( 'fn:local-name(.)', 'VARCHAR(50)' ) AS [name],
    x.f.value( '.', 'VARCHAR(255)' ) AS [value]
FROM (
    
    /* Insert your T-SQL here... */
    SELECT CAST ( ( SELECT
        InstrumentID, InstrumentType, InstrumentNumber, NANumber, DateTimeFiled, [Name]
    FROM @Data
    FOR XML PATH( 'Data' ) ) AS XML ) AS DataXml

) AS d
CROSS APPLY d.DataXml.nodes( '//Data/*' ) x( f );

Returns

+------------------+------------------------------------+
|       name       |               value                |
+------------------+------------------------------------+
| InstrumentID     | 1625168                            |
| InstrumentType   | ACCOUNTS RECEIVABLE                |
| InstrumentNumber | 1                                  |
| NANumber         | 785401                             |
| DateTimeFiled    | 1989-01-03T09:48:03                |
| Name             | HIBERNIA NATIONAL BANK NEW ORLEANS |
| InstrumentID     | 1382385                            |
| InstrumentType   | SALE                               |
| InstrumentNumber | 1                                  |
| NANumber         | 785467                             |
| DateTimeFiled    | 1989-01-03T10:14:03                |
| Name             | M & M RESTAURANT AND LOUNGE, INC   |
+------------------+------------------------------------+
critical_error
  • 6,306
  • 3
  • 14
  • 16
  • Thank you for taking the time out to help me. I edited my question to clarify what I am trying to do. Based on what I initially asked you gave me the correct answer. Would be able to check my edit and see if you could help me once again? THANK YOU – BL-OOP Oct 01 '20 at 13:54