sql 换行符处理

 

There are times when you need to insert some special characters such as “newline”,”tab”
etc as values into a field. Usually every one tries with ‘\n’ or ‘\t’, in vain.
we can do this by using the ASCII value of the character. The steps are very simple.

if you want to insert a special character find the ASCII value of that character like ‘ 9 for tab’, ‘ 12 for new line’ etc.
you can find the lists of characters with its ASCII values at
http://awesomesql.wordpress.com/2009/08/10/ascii-character-set-table.

Once this is done, concatenate your string with the character of that particular ASCII value as
‘my string and’+ char(12)+’someting’

this will be

my string and

something


an examle is as shown below

INSERT INTO TABLES( FIELDS)

下面这句非常关键
SELECT ‘hello’+char(9)+’:'+char(12)+’this is a new line’

this will be stored in the table as
-------------------------

    hello    :

    this is a new line

--------------------------

in the above example I inserted a tab after hello and a new line after ‘:’

you can similarly use
下面是特殊字符所在的accii码

new line = char(12)

space = char(32)

horizontal tab = char(9)

carriage return = char(15)

vertical tab = char(13)

end of text = char(3)


sqlite 换行符号的处理
插入时: 'A' || X'0A' || 'B' 字符串A,B之间用 换行符连接起来

java 取出时 已经自动变成\n, 可以自动换行了。
当然也可以接去掉, 看个人需要
select replace(os_version, x'0a','\n') from tb_scan_host, 可以转换

 

附:editplus 替代特殊字符

 

There are times when you need to insert some special characters such as “newline”,”tab”
etc as values into a field. Usually every one tries with ‘\n’ or ‘\t’, in vain.
we can do this by using the ASCII value of the character. The steps are very simple.

if you want to insert a special character find the ASCII value of that character like ‘ 9 for tab’, ‘ 12 for new line’ etc.
you can find the lists of characters with its ASCII values at
http://awesomesql.wordpress.com/2009/08/10/ascii-character-set-table.

Once this is done, concatenate your string with the character of that particular ASCII value as
‘my string and’+ char(12)+’someting’

this will be

my string and

something


an examle is as shown below

INSERT INTO TABLES( FIELDS)

下面这句非常关键
SELECT ‘hello’+char(9)+’:'+char(12)+’this is a new line’

this will be stored in the table as
-------------------------

    hello    :

    this is a new line

--------------------------

in the above example I inserted a tab after hello and a new line after ‘:’

you can similarly use
下面是特殊字符所在的accii码

new line = char(12)

space = char(32)

horizontal tab = char(9)

carriage return = char(15)

vertical tab = char(13)

end of text = char(3)


sqlite 换行符号的处理
插入时: 'A' || X'0A' || 'B' 字符串A,B之间用 换行符连接起来

java 取出时 已经自动变成\n, 可以自动换行了。
当然也可以接去掉, 看个人需要
select replace(os_version, x'0a','\n') from tb_scan_host, 可以转换

附:editplus 替代特殊字符

3. 替代特殊字符 \r\n \r\n\r\n
    3.1 清除\r\n\r\n
    查找: (.)+(\\r\\n\\r\\n)(.)*
    替换: \1' || X'0A' || X'0A' || '\3

 

    3.2 清除\r\n
    查找: ([^\\n])+(\\r\\n)([^\\r])*
    替换: \1' || X'0A' || '\3

 

 

 

 

 

 

在 Oracle SQL处理换行符时,通常会涉及到识别和替换不同系统下的换行格式,如 Unix/Linux 中的 `\n`(CHR(10))或 Windows 中的 `\r\n`(CHR(13)||CHR(10))[^1]。以下是几种常见的处理方式: ### 替换换行符 #### 使用 `REPLACE` 函数 如果换行符是 Unix/Linux 风格的 `\n`,可以使用 `REPLACE` 函数直接替换: ```sql SELECT REPLACE(my_column, CHR(10), ';') AS modified_column FROM my_table; ``` 如果换行符是 Windows 风格的 `\r\n`,则需要进行两次替换:首先替换 `\r\n`,然后处理可能残留的 `\n`: ```sql SELECT REPLACE(REPLACE(my_column, CHR(13)||CHR(10), ';'), CHR(10), ';') AS modified_column FROM my_table; ``` #### 使用 `REGEXP_REPLACE` 函数 对于更灵活的替换需求,可以使用 `REGEXP_REPLACE` 函数,通过正则表达式匹配所有换行符: ```sql SELECT REGEXP_REPLACE(my_column, '[\r\n]+', ';') AS modified_column FROM my_table; ``` 该方法可以同时处理 `\r\n` 和 `\n`,并将其替换为指定字符(如分号 `;`)[^1]。 ### 删除换行符 如果目标是删除换行符而非替换,可以使用 `REPLACE` 函数将其替换为空字符: ```sql SELECT REPLACE(my_column, CHR(10), '') AS modified_column FROM my_table; ``` 对于 Windows 风格的换行符 `\r\n`,同样需要进行两次替换: ```sql SELECT REPLACE(REPLACE(my_column, CHR(13)||CHR(10), ''), CHR(10), '') AS modified_column FROM my_table; ``` ### 特殊符号 ASCII 定义 - 制表符:`CHR(9)` - 换行符:`CHR(10)` - 回车符:`CHR(13)`[^2] 这些定义在处理文本数据时非常有用,特别是在需要识别或删除特殊字符时。 ### 查找换行符 如果需要查找换行符的存在,可以使用 `TRANSLATE` 函数结合 `CHR` 函数进行检测: ```sql SELECT TRANSLATE(string, CHR(13)||CHR(10), ',') FROM dual; ``` 此方法可以将换行符转换为其他字符(如逗号 `,`),便于进一步分析[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值