Insert multi-line text to SQL table

Posted on 2009-04-09
Last Modified: 2012-05-06
I have multiple textarea fields that upon submit need to keep the carriage returns, tabs, spaces, etc. when it is saved to the SQL table.  The data type I have for the fields is 'ntext'.

What do I need to do so that the carriage returns and tabs, etc. are sent to the table and displayed properly when shown on the page.
Here are examples of the form field and the insert code:
<cftextarea name="strDetail"
    message="You must enter a detailed description of the request." />
<cfquery datasource="mydns" name="insertexample">
     insert into MY_table
          '#DateFormat(Now(), 'mm/dd/yy')# #TimeFormat(Now(), 'hh:mm:ss tt')#', 

Open in new window

Question by:Lee R Liddick Jr
  • 7
  • 6
LVL 21

Expert Comment

by:Craig Wagner
ID: 24107680
You shouldn't have to do anything special. I save text with CR/LF all the time and it is preserved in the database. Or are you talking about the soft wrapping that results in a multi-line text box? That soft wrapping doesn't cause CR/LF characters to be inserted into the text. In that case you're going to have to process the text and do the line-breaking in code before storing the data in the database.

Author Comment

by:Lee R Liddick Jr
ID: 24107873
When it is displayed on the page it lumps everything right after each other.  In the database field it also displays it as one long text string.

In the table field it displays like this:
Set up new stuff.  When the user activates one of these it will show one of the following: Something 1 Something 2 Something 3 Something 4.  Please do something.

When it should look like this in the table and/or when displayed from a query:

Set up new stuff.  When the user activates one of these it will show one of the following:
     Something 1
     Something 2
     Something 3
     Something 4

You wrote about processing the text and doing the line-breaking in code before storing the data in the database...if that is the case, how exactly is that done as I have never had experience in doing that before.  

I even upped the point value since this is more than a simple answer.
LVL 21

Expert Comment

by:Craig Wagner
ID: 24108213
You say, "In the table field it displays like this." Are you talking about in query results from SQL Server Management Studio? Are you using Grid or Text view for the results? Grid view will never show the line breaks, it always puts everything in a single row. If there are CR/LF characters in the string, Text view of the results would show you the actual formatting.
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.


Author Comment

by:Lee R Liddick Jr
ID: 24108843
No query...just opening up the table in SSMS and looking in the column.  I have queries that are displayed on the web...when it displays on the web, all slammed together like I displayed above.
LVL 21

Accepted Solution

Craig Wagner earned 500 total points
ID: 24109680
When you open the table in SSMS you are executing a query (click on the little SQL button and you'll be able to edit the query) and displaying it in a grid. You'll never see the CR/LF rendered in that format, it will always appear as a single line.

When you display the queries "on the web" are you displaying them in a textarea or other control, or just writing them to the page? If the latter, you won't see the CR/LF there either, because HTML doesn't honor CR/LF characters. That's just the way HTML works. You'd have to put the text into a multi-line control (textarea) in order to see the effect.

The first thing you have to figure out is whether or not the CR/LF data is actually in the database. So far nothing you've done would seem to conclusively prove it isn't there.

Go into SSMS. Open a new query window. Write your query (select * from whatever). Before executing the query, go to Query > Results To > Results To Text. That will show you definitely whether the data in the database contains the CR/LF or not.

If the data were entered the way you showed above, as multiple lines in the textarea, you should see the results breaking across multiple lines. If you don't, then the CR/LF characters are not making it from the UI into the database, so the next step I'd do is use SQL Profiler to view the traffic between the application and database and see if the data is actually being passed through with the CR/LF characters intact.
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 500 total points
ID: 24110088
Here's an example of what I'm trying to illustrate.

I created a table with two columns, an integer and a varchar(50). I then ran the following SQL:

insert into junk
values(3, 'this is some text
that contains
line breaks')

As you can see from the attached images, when I view the results of a query against the row (i.e. select * from junk where primarykey = 3) I see a single row in grid view but I see the line breaks in text view.


Author Comment

by:Lee R Liddick Jr
ID: 24112606
I think I found what part of my problem is.  I did what you had above and then looked further into my code.  The form actually goes to a validation page for the end user to validate what they entered...if it's wrong, they return to the form, correct what they had and resubmit it.  If it's okay, then they just submit it.  But I have the form data saved as variables on this page and then submit to the database.  For example:

<cfparam name="variables.strDetail" type="string" default="#form.strDetail#" />
<!--- this is the code that displays the data from the form --->
    <td style="text-align:right; font-weight:bold; width:25%;">Description:</td>
    <td colspan="3">#variables.strDetail#</td>
<!--- then if end user hits submit, this is what gets sent to the database for insert --->
<cfinput type="hidden" name="strDetail" value="#strDetail#" />

Open in new window

LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 500 total points
ID: 24112670
That HTML is definitely not going to show line breaks, because you're basically just inserting the content as text into the HTML document, and as I said before, HTML doesn't honor CR/LF, if you want a line break in HTML you need to use <br>. If you want the validation/confirmation page to show the line breaks, you're going to have to do a substitution on the string to replace any CR and/or LF with <br> to get it to display properly. Either that or put the data into a disabled textarea.

Author Comment

by:Lee R Liddick Jr
ID: 24112691
I did try to put just that field into a disabled textarea box but I couldn't get the data to come back right if the end user chose to go back to the form instead of submitting.  You know of any other type of validation I can use.  I found this one off the internet but it seems to not be doing what I need.  Any suggestions on that?

Author Comment

by:Lee R Liddick Jr
ID: 24149671
Is this happening because it is a flash form?  I had to go out to the internet to figure out how to write the code to even get the CR's in.  I got it partly working but there is still something that is not right.  

Author Comment

by:Lee R Liddick Jr
ID: 24171926
I've tried CR, chr(10, and char(10) to convert the CR's to <br />'s and none of that is working...what is it supposed to be?  This is very frustrating.
LVL 21

Expert Comment

by:Craig Wagner
ID: 24250656
I put quite a bit of effort into putting together examples showing that the carriage returns are being stored and retrieved from the database (which was the first part of the question).

The thread then morphed into how to convert the carriage returns into HTML line break tags, which I could not help with because I do not know ColdFusion (this wasn't tagged as a ColdFusion question to begin with).

I think at least part of the points should be awarded for putting the OP on the right track.

Author Comment

by:Lee R Liddick Jr
ID: 24250849
I have no problem with that Craig...I would have just awarded the points without posting the delete but after my last three posts with no response, I figured I would get your response with the delete.  I appreciate all the assistance with the beginning part of this as it was initially thought it was a SQL issue as to why it wasn't putting the CR's in.  Thanks again and I will be posting the points here shortly.  Pool guys are here now...thanks again.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Backup skipping a few tables 7 51
How to simplify my SQL statement? 14 55
Query 14 57
SQL Error - Query 6 41
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question