Replace line breaks in sql

Reprinted from: http://blog.csdn.net/ljsososo/article/details/10378573

Carriage returns and line feeds usually appear in rich text content. In the sql database, these carriage returns and line feeds appear as spaces after outputting html.

This is found in data export and import. Usually, the carriage return and line feed are found and replaced with <br>. 

Here, the sql function is used. replace(string_expression , string_pattern , string_replacement), the first parameter: the field to look for. Second parameter: the character to look for. The third parameter: the character to be replaced with.

char(9) horizontal tab character
char(10) newline key
char(13) Enter key

1> carriage return char(13)

SELECT *, REPLACE(detail, CHAR(13) , '<br>') AS display the replaced content FROM Test

2> line break

SELECT *, REPLACE(detail, CHAR(10), '<br>') AS display the replaced content FROM Test

3> carriage return line feed

SELECT *, REPLACE(detail, CHAR(13) + CHAR(10), '<br>') AS display the replaced content FROM Test

4> Replace the carriage return and line feed with <BR><BR>

UPDATE TestSET detail = REPLACE(detail, CHAR(13) + CHAR(10), '<br><br>') 

update t_news set content=REPLACE(content, CHAR(13) + CHAR(10), '<br><br>') where news_type=3

Add two spaces before the content, full-width update t_news set content=' '+content where news_type=3

===========================

How to use sql statement to determine whether a field contains a newline?

 
select * from table name where instr(field name, chr(13))>0
In the base chr(13) represents a newline.

In sql server 2000, the instr function is not supported, just change instr to charindex.

Introduction to the charindex function
1. Grammar
CHARINDEX ( char1 ,string1 [ , start_location ] )
If one of char1 or string1 is a Unicode data type (nvarchar or nchar) and the other is not, convert the other to a Unicode data type . CHARINDEX cannot be used with text, ntext, and image data types .
CHARINDEX returns NULL if either char1 or string1 is NULL and the database compatibility level is 70 or higher. If the database compatibility level is 65 or lower, CHARINDEX will only return a NULL value if both char1 and string1 are NULL.
CHARINDEX returns 0 if string1 is not found within char1.
char1 An expression containing the sequence of characters to find.
string1  一个表达式,通常是一个为指定序列搜索的列。string1 属于字符串数据类别。
start_location  开始在 string1 中搜索 char1 时的字符位置。
如果 start_location 未被指定、是一个负数或零,则将从 string1 的开头开始搜索。start_location 可以是 bigint 类型。
 string1 中包含 char1 时返回字符位置
 string1 中不包含 char1 时返回0
二、举例
USE AdventureWorks
SELECT CHARINDEX('bicycle', DocumentSummary)
FROM Production.Document
WHERE DocumentID = 3;
返回结果为48。
SELECT CHARINDEX('bicycle1', DocumentSummary, 5)
FROM Production.Document
WHERE DocumentID = 3;
返回结果为0。
查询DocumentSummary字段中包含"bicycle"的所有行。
一般大家都会写成这样:
select * from Production.Document
 where DocumentSummary like'%bicycle%'
了解这个函数以后,大家可以这样写:
select * from Production.Document 
where charindex('bicycle',DocumentSummary)>0 这种方法比like'%%'的形式速度上要快很多.
数据库优化的时候可以考虑使用sql 2005的函数.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326779004&siteId=291194637