在Oracle SQL表中查找带双引号的字段

时间:2019-07-15 01:43:55

标签: sql oracle

我在表中有一个VARCHAR2(8)字段,当我在Oracle SQL Developer中查询它时,得到两个不同的结果。例如,

SELECT BRAND_NAME FROM BRAND WHERE BRAND_NAME = 'TOYOTA';

返回1行:

|TOYOTA|
SELECT BRAND_NAME FROM BRAND WHERE BRAND_NAME LIKE '%TOYOTA%';

返回两行:

|TOYOTA|
|TOYOTA|

这些值在Oracle SQL Developer中看起来相似,但是当我将其复制并粘贴到记事本中时,得到的输出如下。

TOYOTA
"TOYOTA "

我需要删除带有引号的值的行,但由于表没有任何键字段,因此无法明确标识该行。 我尝试使用转义字符进行搜索,但未成功。

2 个答案:

答案 0 :(得分:0)

您可以使用以下命令找到字符串:

Background Notification

(将SELECT BRAND_NAME FROM BRAND WHERE BRAND_NAME = 'TOYOTA '; 字符串的内容从记事本复制/粘贴到上面的查询,而不是尝试重新输入并获取不正确的空格字符)

如果找不到正确的最终字符,请使用正则表达式匹配字符串后的空白字符:

"TOYOTA "

SELECT BRAND_NAME FROM BRAND WHERE REGEXP_LIKE( BRAND_NAME, 'TOYOTA\s' ); 字符串的内容以查找附加字符的字符代码:

DUMP

哪个会输出:

BRAND_NAME     | DUMP(BRAND_NAME)                
:------------- | :-------------------------------
TOYOTA         | Typ=1 Len=6: 84,79,89,79,84,65  
TOYOTA         | Typ=1 Len=7: 84,79,89,79,84,65,9

db <>提琴here

您会看到一个额外的字符,其字符代码为9(制表符)

要删除该行,可以使用:

SELECT BRAND_NAME, DUMP( BRAND_NAME )
FROM   BRAND
WHERE  BRAND_NAME LIKE '%TOYOTA%';

DELETE FROM BRAND
WHERE  BRAND_NAME = 'TOYOTA ' -- Copy/paste the value from notepad, don't retype it

DELETE FROM BRAND
WHERE  BRAND_NAME = 'TOYOTA' || CHR(9) -- Use exact character code

(确保最后一个不匹配太多行)

db <>提琴here

答案 1 :(得分:0)

Dense Rank将获得具有重复值和不同长度的记录的列名和长度,这些长度可用于delete语句。例如:

with BRAND as 
(      select 1 as ID, 'TOYOTA'  as BRAND_NAME from dual 
 union select 1 as ID, 'TOYOTA ' as BRAND_NAME from dual 
 union select 2 as ID, 'HONDA'   as BRAND_NAME from dual 
 union select 2 as ID, 'HONDA'   as BRAND_NAME from dual 
 union select 3 as ID,'CORVETTE' as BRAND_NAME from dual 
 union select 3 as ID,'CORVETTE 'as BRAND_NAME from dual)
 select lenth,BRAND_NAME from (
 select Dense_Rank() over(partition by trim(j.BRAND_NAME)
 order by length(j.BRAND_NAME) asc) as ct,
 length(j.BRAND_NAME) as lenth, j.* from BRAND j) 
 where ct!=1;