像运算符regex不能在Posgresql中按预期方式工作 [英] Like Operator regex not working as expected in Posgresql
本文介绍了像运算符regex不能在Posgresql中按预期方式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表中有以下数据
HJ-DEF-ABCF010-ABC18-09-17-D
GHJ-ABC-ABFV006-ABC18-09-18-R
OH-DEF-ABFCRT2037-ABC17-01-18-R
我想在另一列中填充值,例如
I want to populate the value in another column like
HJ-DEF-ABCF010-ABC18-09-17-D BET
GHJ-ABC-ABFV006-ABD18-09-18-R BET
OH-DEF-ABFCRT2037-ABCD17-01-18-R BET
作为
ABC18 is BET
ABD18 is BET
ABCD17 is BET
select col1,case
when col1 like '%-ABC[1-2][0-9]-%' then BET
when col1 like '%-ABD[1-2][0-9]-%' then BET
when col1 like '%-ABCD[1-2][0-9]-%' then BET
else - end form table
这在SQl服务器上运行良好,但在Pogresql中我们无法使用[1-2]来找到pos中的期望数字位置。任何建议或想法如何在Posgresql中实现。
which is working fine in SQl sever but in Pogresql we can't use [1-2] to find out the expected digit in the position. Any suggestion or idea how to achieve in Posgresql.
推荐答案
您可以使用 类似于TO
非常类似于您的 Like
的功能,因为它允许某些POSIX正则表达式模式。您甚至可以将模式合并为1个模式:
You may use SIMILAR TO
that will be very close to what your LIKE
does as it allows some POSIX regex patterns. You may even merge the patterns into 1 pattern:
select col1,case
when col1 SIMILAR TO '%-AB([CD]|CD)[1-2][0-9]-%' then BET
else - end from table
$结尾b
$ b
详细信息
-
%
-任意0个以上的字符 -
-AB
-一个-AB
子字符串 -
([CD] | CD)
-要么是C
或D
,或CD
-
[1-2]
-1
或2
-
[0-9]-
-一个数字后跟-
-
%
-直到记录末尾的任何文本。
%
- any 0+ chars-AB
- an-AB
substring([CD]|CD)
- either aC
, orD
, orCD
[1-2]
-1
or2
[0-9]-
- a digit followed with-
%
- any text up to the end of the record.
这篇关于像运算符regex不能在Posgresql中按预期方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文