Share via

Format Data returned by TSQL into HTML

vsslasd 556 Reputation points
2020-10-20T21:19:33.723+00:00

I have the following query

Select
Trim(AP200.VENDORID) as VendorId,
Trim(AP300.ADRSCODE) as VendorLocId,
Trim(AP200.VendName) as VendorName,
CSZ=Concat(
'<p>',Trim(AP300.Address1) , '</p>',
'<p>',Trim(AP300.Address2) , '</p>',
'<p>',Trim(AP300.Address3) , '</p>',
'<p>',Trim(AP300.City) , ', ',Trim(AP300.State) , ' ',Trim(AP300.Zipcode), '</p>')

from
dbo.AP200 AP200 (nolock)
Left Join dbo.AP300 AP300 (nolock) on
AP200.VENDORID=AP300.VENDORID
Where AP300.ADRSCODE not like 'REMIT%'

  • which I am just looking to return so it can be used in HTML format whereby all of the address information is concatenated and has a carriage return after each address line. What is the best way to write the TSQL statement ?

Also - how do I handle if there is no information for Address2 or Address3 ?

Thank you!

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


4 answers

Sort by: Most helpful
  1. Viorel 126.9K Reputation points
    2020-10-22T08:52:15.513+00:00

    If the formatting rules or styles of your HTML require <p> elements, then consider this expression too:

    CSZ = Concat(
       '<p>' + nullif(Trim(AP300.Address1), '') + '</p>',
       '<p>' + nullif(Trim(AP300.Address2), '') + '</p>',
       '<p>' + nullif(Trim(AP300.Address3), '') + '</p>',
       '<p>', Trim(AP300.City), ', ', Trim(AP300.State), ' ', Trim(AP300.Zipcode), '</p>' )
    

    The empty or null address lines will be excluded. It is also possible to adjust the last line to avoid unneeded ‘,’ in case of missing City.

    Was this answer helpful?

    0 comments No comments

  2. EchoLiu-MSFT 14,626 Reputation points
    2020-10-21T09:02:11.613+00:00

    Hi @vsslasd

    Add a carriage return after each address, you can use char(13)(CHAR (Transact-SQL)):

    Select   
    Trim(AP200.VENDORID) as VendorId,   
    Trim(AP300.ADRSCODE) as VendorLocId,   
    Trim(AP200.VendName) as VendorName,  
    CSZ=Concat(  
    '<p>',Trim(AP300.Address1)+ CHAR(13) , '</p>',  
    '<p>',Trim(AP300.Address2)+ CHAR(13) , '</p>',  
    '<p>',Trim(AP300.Address3)+ CHAR(13) , '</p>',  
    '<p>',Trim(AP300.City) , ', ',Trim(AP300.State) , ' ',Trim(AP300.Zipcode), '</p>')  
      
    from   
    dbo.AP200 AP200 (nolock)  
    Left Join dbo.AP300 AP300 (nolock) on   
    AP200.VENDORID=AP300.VENDORID  
    Where AP300.ADRSCODE not like 'REMIT%'  
    

    Your question is not very clear to me. I suggest that you post the question with tables and data and the output you expect.
    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  3. Yitzhak Khabinsky 27,116 Reputation points
    2020-10-20T21:59:56.68+00:00

    Hi @vsslasd ,

    HTML is a subset of XML. Here is how to compose your address section via XQuery.

    SQL

    -- DDL and sample data population, start  
    DECLARE @tbl TABLE (  
     ID INT IDENTITY PRIMARY KEY,  
     Address1 VARCHAR(50),   
     Address2 VARCHAR(50),   
     Address3 VARCHAR(50),  
     City VARCHAR(20),  
     State VARCHAR(20),  
     ZipCode VARCHAR(9)  
    );  
    INSERT INTO @tbl (Address1, Address2, Address3, City, State, ZipCode)  
    VALUES  
    ('38 SW 52nd Street', NULL, NULL, 'Miami', 'FL', '33180'),  
    ('21170/7 Hollywood Street', 'Mapleridge', NULL, 'Austin', 'TX', '78610');  
    -- DDL and sample data population, end  
    
      
    SELECT ID  
     , CAST(N'' AS XML).query('<div>  
     <p>{sql:column("Address1")}</p>  
     <p>{sql:column("Address2")}</p>  
     <p>{sql:column("Address3")}</p>  
     <p>{concat(sql:column("City"), ", ", sql:column("State"), " ",sql:column("ZipCode") )}</p>  
     </div>').query('for $x in /div/p[text()] return $x') AS XHTML  
    FROM @tbl;  
    

    Was this answer helpful?

    0 comments No comments

  4. Erland Sommarskog 134.4K Reputation points MVP Volunteer Moderator
    2020-10-20T21:46:22.103+00:00

    I'm not sure that I understand entirely what you want, but this is how you can handle that address2 and address3 may be null/blank:

    CSZ='<p>' + concat_ws('<BR/>', Trim(AP300.Address1), 
                                   Trim(nullif(AP300.Address2, '')),  
                                   Trim(nullif(AP300.Address3, '')), 
                                   Trim(AP300.City) + ', '  + Trim(AP300.State) + ' ' + Trim(AP300.Zipcode)) + '</p>'
    

    The first argument to concat_ws is a separator, but it is not added for null inputs.

    I think <BR/> is a better tag than <P> to get a single CR-LF, with <P> that could be a double. Although that depends on your style-sheet.

    It is worth noting that this not what SQL Server is really target for; HTML formatting is best done client side.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.