SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Building a Comma Separated on a new line


Building a Comma Separated on a new line

Author
Message
Tava
Tava
SSC Veteran
SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

Group: General Forum Members
Points: 286 Visits: 777
Hi All,

I need some help! firstly to tweak my results to work as I expect it to & secondly provide an alternate method if my logic is not best practice.

I'm returning a list of PDF's (very minimal results < 20) into a temporary table and then running the following query to build these into a string. (E.G. 1.pdf,2.pdf,3.pdf ... 20.pdf)


DECLARE @PDFFileName VARCHAR(8000) =
(
SELECT STUFF(( SELECT ', ' + PDF_FileName + '.pdf'
FROM @PDFResult
ORDER BY 1 ASC FOR XML PATH('')),1,1,'')
)




My aim is to split these up 1 per line instead of a massive line (as below)

1.pdf,
2.pdf
...
20.pdf

Any ideas?
Tava
Tava
SSC Veteran
SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

Group: General Forum Members
Points: 286 Visits: 777
forgot to add some important details. reason I'm doing it this way is because I'm passing the variable @PDFFileName into a email template using:


REPLACE(REPLACE(HTML,'[[FileName]]',@ProcessingFile),'[[PDF_FileName]]',@PDFFileName)


Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39144 Visits: 38520
Tava (10/3/2013)
Hi All,

I need some help! firstly to tweak my results to work as I expect it to & secondly provide an alternate method if my logic is not best practice.

I'm returning a list of PDF's (very minimal results < 20) into a temporary table and then running the following query to build these into a string. (E.G. 1.pdf,2.pdf,3.pdf ... 20.pdf)


DECLARE @PDFFileName VARCHAR(8000) =
(
SELECT STUFF(( SELECT ', ' + PDF_FileName + '.pdf'
FROM @PDFResult
ORDER BY 1 ASC FOR XML PATH('')),1,1,'')
)




My aim is to split these up 1 per line instead of a massive line (as below)

1.pdf,
2.pdf
...
20.pdf

Any ideas?


Try this:


DECLARE @PDFFileName VARCHAR(8000) =
(
SELECT STUFF(( SELECT ', ' + PDF_FileName + '.pdf' + char(13) + char(10)
FROM @PDFResult
ORDER BY 1 ASC FOR XML PATH('')),1,1,'')
)



Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Tava
Tava
SSC Veteran
SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

Group: General Forum Members
Points: 286 Visits: 777
I read about using the CHAR(13) + CHAR(10) and i tried that but no difference.... I then read to just use CHAR(10) and that also failed to work.

There were no errors, just didnt work as expected.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16404 Visits: 19080
Where are you checking the results? The grid from SSMS won't show line feeds.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Edward Boyle-478467
Edward Boyle-478467
SSC-Enthusiastic
SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)SSC-Enthusiastic (114 reputation)

Group: General Forum Members
Points: 114 Visits: 1151
Try this

DECLARE @PDFFileName VARCHAR(8000) =
(
SELECT STUFF(( SELECT ', ' + PDF_FileName + '.pdf' + char(13) + char(10)
FROM @PDFResult
ORDER BY 1 ASC FOR XML PATH(''),type).value('(./text())[1]','Varchar(8000)'),1,1,'')
)
Select @PDFFileName
Tava
Tava
SSC Veteran
SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

Group: General Forum Members
Points: 286 Visits: 777
Luis Cazares (10/7/2013)
Where are you checking the results? The grid from SSMS won't show line feeds.


I'm checking the results in the actual out of the email, not the grid in SSMS.
mister.magoo
mister.magoo
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4112 Visits: 7865
It sounds to me like your email may have an html body, in which case, replace the char(13), char(10) combination with a '< br / >' (remove the spaces) tag, which has the same effect on an html page.

MM


select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Tava
    Tava
    SSC Veteran
    SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

    Group: General Forum Members
    Points: 286 Visits: 777
    Edward Boyle-478467 (10/8/2013)
    Try this

    DECLARE @PDFFileName VARCHAR(8000) =
    (
    SELECT STUFF(( SELECT ', ' + PDF_FileName + '.pdf' + char(13) + char(10)
    FROM @PDFResult
    ORDER BY 1 ASC FOR XML PATH(''),type).value('(./text())[1]','Varchar(8000)'),1,1,'')
    )
    Select @PDFFileName



    hmmm, it didn't work still displays in my email as a long concatenated string " a.pdf,b.pdf,c.pdf "

    I'm not too fussed about getting it working as it doesn't look too bad but now I'm just curious for a solution.
    Tava
    Tava
    SSC Veteran
    SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)SSC Veteran (286 reputation)

    Group: General Forum Members
    Points: 286 Visits: 777
    mister.magoo (10/8/2013)
    It sounds to me like your email may have an html body, in which case, replace the char(13), char(10) combination with a '< br / >' (remove the spaces) tag, which has the same effect on an html page.


    Yeah it is HTML body, I've tried using the br or p syntax and no difference.
    Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum

































































































































































    SQLServerCentral


    Search