I have used single column to store multiple comments . In that column i have to store every comments in new line so that i can able to differentiate the comments.
I have tried CHAR(13) and CHAR(13) + CHAR(10) between two strings . But it's not working.I shown the records in single line.
Tried code:
DECLARE #text NVARCHAR(100)
SET #text = 'This is line 1.' + CHAR(13) + 'This is line 2.'
SELECT #text
Please suggest the solution.
You can use PRINT statement instead of SELECT statement to achieve what you want.
For example, you can use any of the followings:
PRINT 'This is line 1.' + CHAR(13) + 'This is line 2.'
Or
PRINT 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'
Or
PRINT CONCAT(N'This is line 1.', 0xd000a, N'This is line 2.')
UPDATE: According to this forum,
You can not see char(13) in SSMS in the Grid format. This character is there and you can see it if you output the result into report, into text, into Excel. But in SSMS grid you can not see this character.
You can change settings from "Results to Grid" to "Results to Text"
from menu using the following steps:
Query -> Results to -> Results to Text
Then you will be able to view line break between two strings using any of the followings
SELECT 'This is line 1.' + CHAR(13) + 'This is line 2.'
Or
SELECT 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'
Or
SELECT CONCAT(N'This is line 1.', 0xd000a, N'This is line 2.')
another way
select concat(N'This is line 1.', 0xd000a, N'This is line 2.')
or
select 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.'
Related
I have a large table of data where some of my columns contain line breaks. I would like to remove them and replace them with some spaces instead.
Can anybody tell me how to do this in SQL Server?
Thanks in advance
SELECT REPLACE(REPLACE(#str, CHAR(13), ''), CHAR(10), '')
This should work, depending on how the line breaks are encoded:
update t
set col = replace(col, '
', ' ')
where col like '%
%';
That is, in SQL Server, a string can contain a new line character.
#Gordon's answer should work, but in case you're not sure how your line breaks are encoded, you can use the ascii function to return the character value. For example:
declare #entry varchar(50) =
'Before break
after break'
declare #max int = len(#entry)
; with CTE as (
select 1 as id
, substring(#entry, 1, 1) as chrctr
, ascii(substring(#entry, 1, 1)) as code
union all
select id + 1
, substring(#entry, ID + 1, 1)
, ascii(substring(#entry, ID + 1, 1))
from CTE
where ID <= #max)
select chrctr, code from cte
print replace(replace(#entry, char(13) , ' '), char(10) , ' ')
Depending where your text is coming from, there are different encodings for a line break. In my test string I put the most common.
First I replace all CHAR(10) (Line feed) with CHAR(13) (Carriage return), then all doubled CRs to one CR and finally all CRs to the wanted replace (you want a blank, I put a dot for better visability:
Attention: Switch the output to "text", otherwise you wont see any linebreaks...
DECLARE #text VARCHAR(100)='test single 10' + CHAR(10) + 'test 13 and 10' + CHAR(13) + CHAR(10) + 'test single 13' + CHAR(13) + 'end of test';
SELECT #text
DECLARE #ReplChar CHAR='.';
SELECT REPLACE(REPLACE(REPLACE(#text,CHAR(10),CHAR(13)),CHAR(13)+CHAR(13),CHAR(13)),CHAR(13),#ReplChar);
I have the same issue, means I have a column having values with line breaks in it. I use the query
update `your_table_name` set your_column_name = REPLACE(your_column_name,'\n','')
And this resolves my issue :)
Basically '\n' is the character for Enter key or line break and in this query, I have replaced it with no space (which I want)
Keep Learning :)
zain
I've searched StackOverflow for all the possible solutions concerning how to insert a linebreak in a SQL text string. I've referred this link but to no avail. How to insert a line break in a SQL Server VARCHAR/NVARCHAR string
But none of the solutions are working for me.
This is what I'm trying to do:
insert into sample (dex, col)
values (2, 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.')
But this is the output generated: (Select Col from sample where dex = 2)
This is line 1. This is line 2.
This is the output that I desire:
This is line 1.
This is line 2.
I'm using SQL server and SSMS if that helps.
Any ideas why it isn't working?
Well your query works perfectly fine.
SSMS by default shows all query out put in the grid view, which does not display the line break character.
To see it you can switch to text view using cntrl + T shortcut or like below
The results I got for your query are below( and they work)
It works perfectly:
CREATE TABLE sample(dex INT, col VARCHAR(100));
INSERT INTO sample(dex, col)
VALUES (2, 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.');
SELECT *
FROM sample;
LiveDemo
Output:
The "problem" is SSMS grid view that skips newline characters (and others too). Otherwise you will get different rows height like in Excel.
You could observe the same behaviour in SEDE.
LiveDemo-SEDELiveDemo-SEDE-TextView
Output:
You could compare it using:
SELECT 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.';
PRINT 'This is line 1.' + CHAR(13)+CHAR(10) + 'This is line 2.';
The CR/LF chars are there, it's just that in the format of your output, they are being ignored.
I've created a fiddle to illustrate this, with 2 VARCHAR columns. In the first one I insert the text with no CR/LF, in the second I include them
CREATE TABLE sample (dex INT, colnocr VARCHAR(50), col VARCHAR(50)) ;
insert into sample (dex, colnocr, col) values
(2,
'This is line 1.' + 'This is line 2.',
'This is line 1.' + CHAR(13) + CHAR(10) + 'This is line 2.'
)
;
if you run the query
SELECT * FROM sample
The result in plain text are:
| dex | colnocr | col |
|-----|--------------------------------|----------------------------------|
| 2 | This is line 1.This is line 2. | This is line 1.
This is line 2. |
but if you run it in tabular :
dex colnocr col
2 This is line 1.This is line 2. This is line 1. This is line 2.
Check it : SqlFiddleDemo
A bit late to this discussion, but in SSMS 2016, there is an option on the Tools | Options menu under Query Results / SQL Server / Results to Grid called "Retain CR/LF on copy or save". Checking this box will allow you to copy values from a cell in a grid result to, say, another query window and still have the line breaks.
I am getting records from database as comma separated. I am getting contact
titles as:
greg w.workcerf, ashir ali, asdddfgjk
This is comma separated has been defined in SQL function getCommaListTitle()
What i want is to get these record on new lines as
greg w.workcerf,
ashir ali,
asdddfgjk
Any idea about what should i use in sql function instead of ','
Append after the comma in getCommaListTitle, CHAR(13) + CHAR(10) for a
new line
CHAR(13) is a new line char and CHAR(10) is a line feed.
See http://msdn.microsoft.com/en-us/library/ms187323.aspx
You should do this in your front end, like on data access layer or may be at presentation layer because your application could be any one a web app or a window app and in both there's different in new line syntax like in web we use <br/> tag whereas in window we use /n.
use the replace function
replace(field, ',', ',' + char(13)+char(10)
...however DO NOT do this in your database, database is about DATA and of course it 'should' be presented in some form... but starting with a line break, and finally you'll end with something like:
SELECT #s = '<tr><td>' + firstname + '</td><td>' + substr(lastname, 1, 30) + '</td></tr>'
FROM ....
RETURN '<table>' + #s + '</TABLE>'
and that is not to route to choose grasshopper
I am trying to save data to the database with Multiline textbox.
and another process is to read from the database and prints to a text file.
My problem is:
1) when I am trying to save the data in the database as new line character its taking unusally number of spaces between two words.
Please can any body help me out how to insert new line character in to the database.
thanks
Char(13) is a carriage return and Char(10) is a line feed.
so...
UPDATE tblEXAMPLE
SET Contents=#FIRSTLINE + chAr(13) + chAr(10)
+ #secondline + chAr(10) + chAr(13)
+ #lastline
WHERE KEY=#PRIMARYKEy
In SQL Server 2005, I want to print out a blank line with the PRINT statement, however, when I run
PRINT ''
it actually prints a line with a single space.
Does anyone know if it's possible to just print a blank line without the space?
If I print a new line character, it doesn't print a space, but I end up with two new lines.
You could just add a newline on your previous print statement, if you have one.
Instead of:
PRINT 'BLABLABLA'
PRINT ''
You could write:
PRINT 'BLABLABLA
' <- the string finishes here!
Very similar to the other suggestion here, this seems to work:
print '
'
-- Search the web for: SQL PRINT NewLine
-- What you'll end up finding:
DECLARE #CR AS CHAR(1) -- Carriage Return (CR)
DECLARE #LF AS CHAR(1) -- Line Feed (LF)
DECLARE #CrLf AS CHAR(2) -- Carriage Return / Line Feed
SET #CR = CHAR(10)
SET #LF = CHAR(13)
SET #CrLf = #CR + #LF
PRINT '--==--==--==--==--=='
PRINT #CrLf + 'Use variables as you see fit' + #CrLf
PRINT '--==--==--==--==--=='
-- AntGut
Can you encode the BACKSPACE character and PRINT that out?
UPDATE: PRINT '' + CHAR(8) doesn't seem to go down particularly well :(
AFAIK there is no way around this, it is the way the print statement works
This suggests that you want to print a blank message. Are you sure that this is your intention? The Print statement actually sends a message to the error/message-handling mechanism that then transfers it to the calling application.