我正在使用Oracle SQL Developer。源表中有一列fiscal_week_string
,其格式如下:
2011 – 01 (09/27 – 10/31)
我想将其转换为以下格式:
2011-01 (09/27 to 10/31)
即,我想删除一些的空格。我还想用“-
”替换大括号内的“to
”。
以下是几行:
datestr pc WEEK_START WEEK_END month_start mnth_end year mth qtr wk fiscal_week_string Fiscal_month_string
2010-09-27 14879 2010-09-27 2010-10-03 2010-09-27 2010-10-31 2011 1 1 1 2011 – 01 (09/27 – 10/03) 2011 – 01 (09/27 – 10/31)
2010-09-28 14880 2010-09-27 2010-10-03 2010-09-27 2010-10-31 2011 1 1 1 2011 – 01 (09/27 – 10/03) 2011 – 01 (09/27 – 10/31)
有人可以指导我吗?
答案 0 :(得分:1)
您可以使用regexp_replace替换目标模式。
示例:
select regexp_replace(
regexp_replace('2011 - 01 (09/27 - 10/31)',' - ','-',1,1)
, ' - ',' to ') from dual;
样本更新:
create table a1(id number, name varchar2(100));
insert into a1 values(1, '2011 - 01 (09/27 - 10/31)');
update a1 set name=
regexp_replace(regexp_replace(name,' - ','-',1,1), ' - ',' to ') where id=1;
select * from a1;
答案 1 :(得分:1)
由于您尚未共享DDL,因此期望DDL
create table table_one
(
fiscal_week_string varchar(30)
);
insert into table_one
values ('2011 – 01 (09/27 – 10/31)');
insert into table_one
values ('2011 – 01 (09/26 – 11/31)');
您的Oracle查询将是
select col_one || col_two || col_three || ' ' || col_four||' '|| 'to'||' '||col_six as fmt_date
from
(
select REGEXP_SUBSTR(t.fiscal_week_string, '[^ ]+', 1, 1) col_one,
REGEXP_SUBSTR(t.fiscal_week_string, '[^ ]+', 1, 2) col_two,
REGEXP_SUBSTR(t.fiscal_week_string, '[^ ]+', 1, 3) col_three,
REGEXP_SUBSTR(t.fiscal_week_string, '[^ ]+', 1, 4) col_four,
REGEXP_SUBSTR(t.fiscal_week_string, '[^ ]+', 1, 5) col_five,
REGEXP_SUBSTR(t.fiscal_week_string, '[^ ]+', 1, 6) col_six
from table_one t
)
和
结果
2011–01 (09/27 to 10/31)
2011–01 (09/26 to 11/31)
答案 2 :(得分:0)
DECLARE @str varchar(50)='2011 – 01 (09/27 – 10/31)'
SELECT REPLACE(SUBSTRING(@str, 0,CHARINDEX('(',@str)),' ','')+' '+ REPLACE(SUBSTRING(@str,CHARINDEX('(',@str),len(@str)),'–','to')