Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data in a table with carriage return?

Tags:

sql

sql-server

In SQL SERVER Is it possible to store data with carriage return in a table and then retrieve it back again with carriage return.

Eg:

insert into table values ('test1 

test2

test3

test4');

When I retrieve it, I get the message in a line

test1 test2 test3 test4

The carriage return is treated as a single character.

Is there way to get the carriage returns or its just the way its going to be stored?

Thanks for the help guys!!!

Edit: I should have explained this before. I get the data from the web development (asp .net) and I just insert it into the table. I might not be doing any data manipulation.. just insert.

I return the data to the app development (C++) and may be some data or report viewer.

I don't want to manipulate on the data.

like image 572
user21968 Avatar asked Sep 26 '08 13:09

user21968


People also ask

What is a carriage return in a data?

In computing, the carriage return is one of the control characters in ASCII code, Unicode, EBCDIC, and many other codes. It commands a printer, or other output system such as the display of a system console, to move the position of the cursor to the first position on the same line.

How do I find a carriage return in SQL?

In SQL Server, we can use the CHAR function with ASCII number code. We can use the following ASCII codes in SQL Server: Char(10) – New Line / Line Break. Char(13) – Carriage Return.

How do you represent a carriage return?

CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line. LF = Line Feed ( \n , 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.


4 Answers

You can store Carriage return in the database. The problem here is that you are using SQL Server Management Studio to display the results of your query. You probably have it configured to show the results in a grid. Change the configuration of SSMS to show results to text and you will see the carriage returns.

Right click in the query window -> Results To -> Results To Text

Run your query again.

like image 135
George Mastros Avatar answered Oct 14 '22 20:10

George Mastros


INSERT INTO table values('test1' + CHAR(10) + 'test2' + CHAR(10) + 'test3' + CHAR(10) + 'test4')

This should do it. To see the effect, switch the query result window to plain text output.

Regards

like image 24
huo73 Avatar answered Oct 14 '22 20:10

huo73


IIRC, using chr(13) + chr(10) should works.

insert into table values ('test1' + chr(13) + chr(10) + 'test2' );
like image 45
Axeman Avatar answered Oct 14 '22 19:10

Axeman


Can you please clarify how you retrieve the data back from the database? What tool do you use? The data probably contains the carriage returns but it's not displayed if you get the results in grid (try the results in text option)

like image 28
Dejan Avatar answered Oct 14 '22 20:10

Dejan