在 Oracle 中使用文字字符(冒号、分号)进行 SELECT 查询

Posted

技术标签:

【中文标题】在 Oracle 中使用文字字符(冒号、分号)进行 SELECT 查询【英文标题】:SELECT query with Literal characters(Colon, Semi-colon) in Oracle 【发布时间】:2019-06-28 05:48:28 【问题描述】:

如何在 Oracle 中查询文字字符为冒号、分号和部分文本的数据?

SELECT result 
  FROM TABLE1 
 WHERE result like '"generalinfo"":""Authorize-All-06262019"'

数据:

Result
"generated":"6/19/2019 12:54:55 PM -05:00","msgid":"SYSTEMID_115064221_2","servicenumber":"115064221","queueid":2,"expected":"7/1/2019 12:00:00 AM -05:00","startrange":"","endrange":"6/24/2019 12:00:00 AM -05:00","startdate":"6/24/2019 12:00:00 AM -05:00","flag":1,"setby":"6/5/2019 12:00:00 AM","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"
"generated":"6/26/2019 11:32:30 PM -05:00","msgid":"SYSTEMID_115064221_2","servicenumber":"115064221","queueid":2,"expected":"7/8/2019 12:00:00 AM -05:00","startrange":"","endrange":"7/1/2019 12:00:00 AM -05:00","startdate":"7/1/2019 12:00:00 AM -05:00","flag":0,"setby":"","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"
"generated":"6/25/2019 6:27:10 AM -05:00","msgid":"SYSTEMID_115064221_3","servicenumber":"115064221","queueid":3,"expected":"7/8/2019 12:00:00 AM -05:00","startrange":"","endrange":"6/24/2019 12:00:00 AM -05:00","startdate":"6/24/2019 12:00:00 AM -05:00","flag":1,"setby":"7/1/2019 12:00:00 AM","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"
"generated":"6/19/2019 12:54:56 PM -05:00","msgid":"SYSTEMID_115047234_2","servicenumber":"115047234","queueid":2,"expected":"7/1/2019 12:00:00 AM -05:00","startrange":"","endrange":"6/24/2019 12:00:00 AM -05:00","startdate":"6/24/2019 12:00:00 AM -05:00","flag":1,"setby":"6/5/2019 12:00:00 AM","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"
"generated":"6/25/2019 6:27:10 AM -05:00","msgid":"SYSTEMID_115047234_3","servicenumber":"115047234","queueid":3,"expected":"7/8/2019 12:00:00 AM -05:00","startrange":"","endrange":"7/1/2019 12:00:00 AM -05:00","startdate":"7/1/2019 12:00:00 AM -05:00","flag":1,"setby":"7/1/2019 12:00:00 AM","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"S@"msgid":"115047234_3","servicenumber":"115047234","queueid":"3","flag":0,"location":"11","generated":"6/26/2019 2:49:02 AM -05:00","type":"FDAAuthorize","generalinfo":"Authorize-All-06262019"
"generated":"6/19/2019 12:54:56 PM -05:00","msgid":"SYSTEMID_115035858_2","servicenumber":"115035858","queueid":2,"expected":"7/1/2019 12:00:00 AM -05:00","startrange":"","endrange":"6/24/2019 12:00:00 AM -05:00","startdate":"6/24/2019 12:00:00 AM -05:00","flag":1,"setby":"6/5/2019 12:00:00 AM","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"
"generated":"6/25/2019 6:27:10 AM -05:00","msgid":"SYSTEMID_115035858_3","servicenumber":"115035858","queueid":3,"expected":"7/8/2019 12:00:00 AM -05:00","startrange":"","endrange":"7/1/2019 12:00:00 AM -05:00","startdate":"7/1/2019 12:00:00 AM -05:00","flag":1,"setby":"7/1/2019 12:00:00 AM","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"S@"msgid":"115035858_3","servicenumber":"115035858","queueid":"3","flag":0,"location":"11","generated":"6/26/2019 1:19:08 AM -05:00","type":"FDAAuthorize","generalinfo":"Authorize-All-06262019"S@"msgid":"115035858_3","servicenumber":"115035858","queueid":"3","flag":0,"location":"11","generated":"6/26/2019 1:20:46 AM -05:00","type":"FDAAuthorize","generalinfo":"Authorize-All-06262019"
"generated":"6/19/2019 12:54:56 PM -05:00","msgid":"SYSTEMID_115036054_2","servicenumber":"115036054","queueid":2,"expected":"7/1/2019 12:00:00 AM -05:00","startrange":"","endrange":"6/24/2019 12:00:00 AM -05:00","startdate":"6/24/2019 12:00:00 AM -05:00","flag":1,"setby":"6/5/2019 12:00:00 AM","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"
"generated":"6/25/2019 6:27:10 AM -05:00","msgid":"SYSTEMID_115036054_3","servicenumber":"115036054","queueid":3,"expected":"7/8/2019 12:00:00 AM -05:00","startrange":"","endrange":"7/1/2019 12:00:00 AM -05:00","startdate":"7/1/2019 12:00:00 AM -05:00","flag":1,"setby":"7/1/2019 12:00:00 AM","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"S@"msgid":"115036054_3","servicenumber":"115036054","queueid":"3","flag":0,"location":"11","generated":"6/26/2019 1:38:55 AM -05:00","type":"FDAAuthorize","generalinfo":"Authorize-All-06262019"
"generated":"6/17/2019 7:50:06 AM -05:00","msgid":"SYSTEMID_116456178_2","servicenumber":"116456178","queueid":2,"expected":"6/24/2019 12:00:00 AM -05:00","startrange":"6/22/2019 12:00:00 AM -05:00","endrange":"6/18/2019 12:00:00 AM -05:00","startdate":"6/18/2019 12:00:00 AM -05:00","flag":1,"setby":"6/3/2019 12:00:00 AM","location":"11","consideredby":"ATS","type":"FDA","generalinfo":"NA"

如何获取具有“generalinfo”值和“NA”而不是“NA”行的行?

【问题讨论】:

你应该看看 Oracle 的 JSON 解析函数。不要尝试对 JSON 内容使用正则表达式。 如果列数据类型不是 JSON 则尝试SELECT result FROM TABLE1 WHERE result like '%"generalinfo"":""Authorize-All-06262019"%' 你的数据库版本是多少? WHERE result like '%"generalinfo":"Authorize-All-06262019"%'??? @BarbarosÖzhan:甲骨文 12 【参考方案1】:

如果您的数据库版本是 12c,那么只要您的列 (result) 的格式符合 json 格式,您就可以通过添加检查约束轻松找出:

alter table table1 
add constraints chk_result_json  
check(result is json);

并检查 generalinfo 不是 NA 为:

select * 
  from table1 t
 where t.result.generalinfo != 'NA'

使用treat(result AS json) as 更容易用于 18c 版本:

select *
  from ( select id, treat(result AS json) as result from table1 ) t
 where t.result.generalinfo != 'NA' 

Demo

【讨论】:

我安装了 DB 12.1.0.2,它也支持上述约束亲爱的@a_horse_with_no_name。

以上是关于在 Oracle 中使用文字字符(冒号、分号)进行 SELECT 查询的主要内容,如果未能解决你的问题,请参考以下文章

为啥 "%.10f" % Decimal(u) 可以发出带有文字冒号的字符串?

Oracle 中的 JDBC 瘦连接字符串同时使用冒号和正斜杠

冒号的解释及造句

ORACLE 如何按逗号截取查询所得的表名

使用冒号、分号等符号将变量传递给 javascript 函数

什么时候需要在 Oracle SQL 中使用分号和斜杠?