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
Related
I have a column that is defined as a nvarchar(100).
Sometimes office users press enter key wrongly and it causes problems in some applications. When I check the value it seems like double space character in SQL Server. Now my mission is finding the records that has this situation. I tried the code below to find the problematic records:
DECLARE #NewLineChar AS CHAR(2) = CHAR(10) + CHAR(13);
SELECT *
FROM TBLCASABIT
WHERE CARI_ISIM LIKE '%'+CHAR(10) + CHAR(13)+'%'
but it doesn't return the records I need.
If I use double space character I can see the test record I put and some other records has double space character. So I can't be sure if other records have the same situation or not.
How can I handle this situation? Any help will be appreciated.
I believe you are getting the order of CHAR(10) and CHAR(13) backwards. Here is what each of these characters is:
CHAR(13) - carriage return, or \r
CHAR(10) - line feed, or \n
In Windows, \r\n is a line ending, while in Unix \n by itself is interpreted as a line ending. So you should be searching for CHAR(13) followed immediately by CHAR(10), and not the other way around. Try the following query:
SELECT *
FROM TBLCASABIT
WHERE CARI_ISIM LIKE '%' + CHAR(13) + CHAR(10) + '%'
References:
MSDN Social
DOS vs. Unix Line Endings
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 would like to consult about the best way to remove a certain substring from a varchar field of every row in a table.
Let's assume I have a single column table, the column names is "user_list" and it is a varchar field that contain user names seperated by ";".
for example:
row1: james;david;moses
row2: mary;moses;terry
row3: ronaldo;messi;zlatan
the lists are not sorted in anyway.
I want to crate a SP that gets a username and removes it from every row it appears,
for instance if the db is the example above and i got as an input 'moses'
I would like it to look like
row1: james;david;
row2: mary;terry
row3: ronaldo;messi;zlatan
I want it to be a single update command and not a cursor, and i'm thinking with myself (and now with you) what is the best way to do it.
Thanks!
You have a very poor data structure. SQL has this great structure for storing lists of things. It is called a "table". In particular, you want a junction table instead of storing values as lists.
That said, you cannot always control how data is structured. The following should help:
update table t
set usernames = replace(replace(';' + usernames + ';', ';' + #UserName + ';', ''), ';;', ';')
where ';' + usernames + ';' like '%;' + #UserName + ';%';
This will put a semicolon at the beginning and the end of the list. If that is a problem, you can remove them using left() or stuff().
EDIT:
To remove the ; at the beginning, use stuff():
update table t
set usernames = stuff(replace(replace(';' + usernames + ';', ';' + #UserName + ';', ''), ';;', ';'), 1, 1, '')
where ';' + usernames + ';' like '%;' + #UserName + ';%';
Okay so I took what Gordon suggested and to resolve the problem i encountered (can be seen in the comments) I did the following (How didn't I think about it in the first place? :( )
update matan_test
SET usernames= replace(replace(mail_list,#UserName+';', ''), #UserName, '')
where usernames like '%'+#UserName+'%';
I want to do a new line in my sql select
I try this but it won't work
SELECT '1' + char(13) + '2'
AND
SELECT '1' + char(13) + char(10) + '2'
it always return 12
thanks for your answers
in fact I try to export data from sql to TXT file
but when I execute the bcp file it return concatenate rows
The new line is in your result. If you copy and paste the result from your query into notepad, for example, you'll see 1, new line, 2. The reason you see 1 2 in your result in the results window is because you are returning 1 row. Notice that len('1' + char(13) + char(10) + '2') = 4, that is because your chars are present, it's just represented as a single row (with multiple lines in the row).
That's just the way SSMS displays the results. If you copy the result out into Notepad, or as Lamak says choose Results to Text you should see the newlines in place.
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