Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7066609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:06:07+00:00 2026-05-28T05:06:07+00:00

I have a text being returned from SQL that may have newlines in it.

  • 0

I have a text being returned from SQL that may have newlines in it. However, since I’m putting the value in between <p></p> tags, it’s ignoring the newlines.

For example if the text in SQL is actually this (with a CRLF between lines):

foobar car
carbar foo

The HTML code becomes:

<p>foobar car
carbar foo</p>

Which is just rendering as:

foobar car carbar foo

Ultimately it seems that I want to detect a newline then add the appropriate HTML such as:

<p>foobar car</p><p>carbar foo</p>

or even

<p>foobar car<br/><br/>carbar foo</p>

Should this be handled on the front end or in the SQL queries returning the data? It seems that it should be handled on the front end code (in my ASP.NET view) but if so, how?

I noticed that there a function in PHP to handle exactly this case: nl2br (http://www.php.net/manual/en/function.nl2br.php) but unfortunately that doesn’t help me 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:06:07+00:00Added an answer on May 28, 2026 at 5:06 am

    It’s generally better to perform this kind of logic is your presentation code then your DB or middle-tier.

    My Recommended approach would be to write some .net code to replace cr and lf with <br/> tags. The code itself is straightforward, here is an example that will emulate your php nl2br function:

    string nl2br(string text)
    {
        return text.Replace("\r", "<br/>").Replace("\n", "<br/>");
    }
    

    You could also perform this transform in SQL using REPLACE() and CHAR() although it is preferable to perform this kind of transform in the presentation layer, not the database layer.

    The SQL approach would be:

    SELECT REPLACE(REPLACE(myStr, char(13), '<br/>'), CHAR(10), '<br/>')
    

    This would replace CHAR(13) and CHAR(10) (cr and lf) with <br/> tags.

    A third option would be to render your text at preformatted text using the <pre> tags although this is really only suitable for a narrow set of use-cases (such as displaying code as used here on StackOverflow)

    e.g.

    <pre>
        foobar car 
        carbar foo
    </pre>
    

    which would render like this:

    foobar car 
    carbar foo
    

    keeping spaces and new lines intact.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have text I am displaying in SIlverlight that is coming from a CMS
I have text stored in SQL as HTML. I'm not guaranteed that this data
I have a dropdownlist that is being populated from a database. That is working
I have a dynamic amount of div's being returned from a query, all with
I have a function that takes 2 returned variables from another function to write
I have the following style defined which is being applied (as text changes to
I have text within a paragraph tag that that fits snug on the bottom
I have text file with some stuff that i would like to put into
I'm having an issue with a string which is being returned from a webservice.
So, I have this code that grabs a bunch of data from the database,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.