Search code examples
databaseasp-classic

Reorder comma separated sentences into new paragraphs


database screenshot

Above is a database that i am uploading to a hosting server to pull the data off of it to a website using .asp code in a HTML file. The sensors field gets the data using lookup from another table and can contain multiple values but these values are separated using a comma ",". I was wondering if there was a way either within the database or on the .asp page to get rid of the comma and sort them in a new paragraph or replace the "," with "< br/ >" between each of the sentences


Solution

  • NOTE CHANGE TO ORIGINAL QUESTION

    The original question was in reference to ASP.NET. After providing a thorough answer it transpired that the author was actually using VBScript and Classic ASP. The final update to this answer reflects his change.

    I have left my original answer just in case it helps someone else with the same issue as originally asked..

    ===================================

    Replace won't be enough if you want to change any of the order; I.E alphabetically.

    One way would be to put the Database string into a list, splitting it at the comma. you can then output this list in any order you want.

    For example.

    to Split into a list

    List<string> myList = yourSensorsString.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
    

    You can then order this however you see fit. Something like:

    myList.Sort();
    

    Then simply go through each of the items, like - where myLbl is perhaps a label control on your page

    foreach ( str in myList) {
        myLbl.Text += str + "<br />";
    }
    

    Or as separate paragraphs, maybe into a Literal

       foreach ( str in myList) {
            myLit.Text += "<p>" + str + "</p>";
        }
    

    Update

    This code example above would be placed in your code-behind. You would get your sensors string from the database, and then split it into a list, then output to the HTML. No need to get your database to perform this sort of logic. I have also assumed C# as you never mentioned what flavour of .net you're using.

    Update 2

    After looking at your fiddle, i edited it slightly to show where you can use this sort of code. Also converted my code to VB. Hope this helps. Can't put any more time into this... Fiddle Is Here

    Update 3

    The original question was for ASP.NET - as it turns out, it's actually Classic ASP the OP is asking about.

    So split your string like this:

    myList=Split(rsLogbook("FieldName"), ",")
    for each str in myList
        document.write(str & "<br />")
    next
    

    Other than that, I can't help any further - lot of time spent on this :)