gpt4 book ai didi

SQL查询以测试字符串值是否包含回车符

转载 作者:行者123 更新时间:2023-12-04 05:32:33 28 4
gpt4 key购买 nike

试图找出测试VARCHAR列值是否以回车结尾的正确方法。试过这个,但是不起作用,数据库是Oracle 11g ...

select name from myTable where name LIKE '%\r' OR name like '%\n'

最佳答案

要查找包含不可打印字符(例如回车符或垂直制表符或行尾)的值,可以使用regexp_like函数。在您要显示行的情况下,特定列的字符串值最后包含回车符的行,可以使用类似的查询。

select *
from your_table_name
where regexp_like(trim(string_column), '[[:space:]]$')

Demo

对评论的回答
Trim函数默认情况下会删除前导和尾随空格,并且不会删除 回车行尾字符。让我们进行一个简单的测试:
SQL> create table Test_Table(
2 id number,
3 col1 varchar2(101)
4 );

Table created

SQL> insert into Test_Table (id, col1)
2 values(1, 'Simple string');

1 row inserted

SQL> commit;

Commit complete

SQL> insert into Test_Table (id, col1)
2 values(1, 'Simple string with carriage return at the end' || chr(13));

1 row inserted

SQL> commit;

Commit complete

SQL> insert into Test_Table (id, col1)
2 values(1, ' Simple string with carriage return at the end leading and trailing spaces' || chr(13)||' ');

1 row inserted

SQL> commit;

Commit complete

SQL> insert into Test_Table (id, col1)
2 values(1, ' Simple string leading and trailing spaces ');

1 row inserted

SQL> commit;

Commit complete

SQL> select *
2 from test_table;

ID COL1
--------------------------------------------------------------------------------
1 Simple string
1 Simple string with carriage return at the end
1 Simple string with carriage return at the end leading and trailing spaces
1 Simple string leading and trailing spaces

SQL>
SQL> select *
2 from test_table
3 where regexp_like(trim(col1), '[[:space:]]$')
4 ;

ID COL1
----------------------------------------------------------------------------------
1 Simple string with carriage return at the end
1 Simple string with carriage return at the end leading and trailing spaces

SQL>

关于SQL查询以测试字符串值是否包含回车符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12712480/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com