数据提取Redshift正则表达式
Data extraction Redshift regular expression
我正在尝试在下面的代码中寻找 * 后跟 SLD 的模式..
SELECT CASE WHEN REGEXP_COUNT(orderstatus,'^\*SLD') <> 0
THEN TO_DATE(REGEXP_SUBSTR(orderstatus, '[0-9]{1,2}/[0-9]{1,2}') || '/2022', 'MM/DD/YYYY')
ELSE orddate
END AS soldorstockdate
FROM DATA;
但是我收到以下错误
数据库报告语法错误:重复运算符之前的正则表达式无效。解析正则表达式时发生错误:'^*>>>HERE>>>SLD'。详情:--------------------------------------------错误: 前面的正则表达式无效
表格和数据的 DDL 如下
Create table input
(orderstatus varchar(100),
orddate date)
insert into input values
('*SLD 05/11/2022','5/1/2022'),
('*SOLD 05/15/2022','5/8/2022'),
('37141 SLD BAM','5/5/2022'),
('*STOCK 05/16/2022','5/3/2022'),
('1277489 STK#39298.32831','5/4/2022')
create table output
(orderstatus varchar(100),
orddate date
soldorstockdate date)
insert into output values
('*SLD 05/11/2022','5/1/2022','5/11/2022'),
('*SOLD 05/15/2022','5/8/2022','5/15/2022'),
('37141 SLD BAM','5/5/2022','5/5/2022'),
('*STOCK 05/16/2022','5/3/2022','5/16/2022'),
('1277489 STK#39298.32831','5/4/2022','5/4/2022')
orderstatus
orddate
Soldorstockdate
*SLD 05/11/2022
5/1/2022
5/11/2022
*SOLD 05/15/2022
5/8/2022
5/15/2022
37141 SLD BAM
5/5/2022
5/5/2022
*STOCK 05/16/2022
5/3/2022
5/16/2022
1277489 STK#39298.32831
5/4/2022
5/4/2022
您必须使用双反斜杠 \
而不是单反斜杠 \
来转义元字符
红移有 POSIX styled escape characters
To search for strings that include metacharacters, such as ‘. * | ? ‘, and so on, escape the character using two backslashes (' \ ').
SELECT *
, REGEXP_COUNT(orderstatus, '^\*SLD') <> 0 -- Used \ here
, CASE
WHEN REGEXP_COUNT(orderstatus, '^\*SLD') <> 0 -- Used \ here
THEN TO_DATE(REGEXP_SUBSTR(orderstatus, '[0-9]{1,2}/[0-9]{1,2}') || '/2022', 'MM/DD/YYYY')
ELSE orddate
END AS soldorstockdate
FROM input;
orderstatus
orddate
flag
soldorstockdate
*SLD 05/11/2022
2022-05-01
true
2022-05-11
*SOLD 05/15/2022
2022-05-08
false
2022-05-08
37141 SLD BAM
2022-05-05
false
2022-05-05
*STOCK 05/16/2022
2022-05-03
false
2022-05-03
1277489 STK#39298.32831
2022-05-04
false
2022-05-04
我正在尝试在下面的代码中寻找 * 后跟 SLD 的模式..
SELECT CASE WHEN REGEXP_COUNT(orderstatus,'^\*SLD') <> 0
THEN TO_DATE(REGEXP_SUBSTR(orderstatus, '[0-9]{1,2}/[0-9]{1,2}') || '/2022', 'MM/DD/YYYY')
ELSE orddate
END AS soldorstockdate
FROM DATA;
但是我收到以下错误
数据库报告语法错误:重复运算符之前的正则表达式无效。解析正则表达式时发生错误:'^*>>>HERE>>>SLD'。详情:--------------------------------------------错误: 前面的正则表达式无效 表格和数据的 DDL 如下
Create table input
(orderstatus varchar(100),
orddate date)
insert into input values
('*SLD 05/11/2022','5/1/2022'),
('*SOLD 05/15/2022','5/8/2022'),
('37141 SLD BAM','5/5/2022'),
('*STOCK 05/16/2022','5/3/2022'),
('1277489 STK#39298.32831','5/4/2022')
create table output
(orderstatus varchar(100),
orddate date
soldorstockdate date)
insert into output values
('*SLD 05/11/2022','5/1/2022','5/11/2022'),
('*SOLD 05/15/2022','5/8/2022','5/15/2022'),
('37141 SLD BAM','5/5/2022','5/5/2022'),
('*STOCK 05/16/2022','5/3/2022','5/16/2022'),
('1277489 STK#39298.32831','5/4/2022','5/4/2022')
orderstatus | orddate | Soldorstockdate |
---|---|---|
*SLD 05/11/2022 | 5/1/2022 | 5/11/2022 |
*SOLD 05/15/2022 | 5/8/2022 | 5/15/2022 |
37141 SLD BAM | 5/5/2022 | 5/5/2022 |
*STOCK 05/16/2022 | 5/3/2022 | 5/16/2022 |
1277489 STK#39298.32831 | 5/4/2022 | 5/4/2022 |
您必须使用双反斜杠 \
而不是单反斜杠 \
来转义元字符
红移有 POSIX styled escape characters
To search for strings that include metacharacters, such as ‘. * | ? ‘, and so on, escape the character using two backslashes (' \ ').
SELECT *
, REGEXP_COUNT(orderstatus, '^\*SLD') <> 0 -- Used \ here
, CASE
WHEN REGEXP_COUNT(orderstatus, '^\*SLD') <> 0 -- Used \ here
THEN TO_DATE(REGEXP_SUBSTR(orderstatus, '[0-9]{1,2}/[0-9]{1,2}') || '/2022', 'MM/DD/YYYY')
ELSE orddate
END AS soldorstockdate
FROM input;
orderstatus | orddate | flag | soldorstockdate |
---|---|---|---|
*SLD 05/11/2022 | 2022-05-01 | true | 2022-05-11 |
*SOLD 05/15/2022 | 2022-05-08 | false | 2022-05-08 |
37141 SLD BAM | 2022-05-05 | false | 2022-05-05 |
*STOCK 05/16/2022 | 2022-05-03 | false | 2022-05-03 |
1277489 STK#39298.32831 | 2022-05-04 | false | 2022-05-04 |