带有空格

时间:2017-02-16 12:45:37

标签: sql regex postgresql substring

我一直在努力尝试在此论坛中找到解决方案的查询,但我无法继续。我需要帮助。 我有一个专栏,通过船的生命存储船名,我想将它们分成三列。 主要是我有这三个选项,

a)只有一个名字

select t2.esp1,t2.espectro,t2.espectro1, t2.id from(
select substring(t.espectro, t.posfin)::varchar as esp1, t.espectro,t.espectro1,t.id from(
select "Id" as id, strpos(shipname, ', ') as posinic, strpos(shipname, ' y ') as posfin,shipname as espectro, shipname1 as espectro1 from ships) t)t2 (esp1, espectro, espectro1, id)
where t2.esp1 not like '% y %'`)

b)两个名字

select t2.esp1,t2.espectro,t2.espectro1, t2.id from(
select substring(t.espectro,1, t.posfin)::varchar as esp1, t.espectro,t.espectro1,t.id from(
select "Id" as id, strpos(shipname, ', ') as posinic, strpos(shipname ' y ') as posfin,shipname as espectro, shipname1 as espectro1 from ships) t)t2 (esp1, espectro, espectro1, id)
where t2.esp1 not like '%, %'`) and for the second name (`select t2.esp1,t2.espectro,t2.espectro1, t2.id from(
select substring(t.espectro, t.posfin)::varchar as esp2, t.espectro,t.espectro2,t.id from(
select "Id" as id, strpos(shipname, ', ') as posinic, strpos(shipname, ' y ') as posfin,shipname as espectro, shipname2 as espectro2 from ships) t)t2 (esp2, espectro, espectro2, id)
where t2.esp2 like '% y %' and t2.espectro not like '%, %';

和c)三个名字:我可以先得到

select substring(t.espectro,1,t.posicion) from(
select strpos(shipname, ',') as posicion,shipname as espectro from ships) t;` and third `select t2.esp3,t2.espectro,t2.espectro3, t2.id from(
select substring(t.espectro, t.posfin)::varchar as esp3, t.espectro,t.espectro3,t.id from(
select "Id" as id, strpos(shipname, ', ') as posinic, strpos(shipname, ' y ') as posfin,shipname as espectro, shipname3 as espectro3 from ships) t)t2 (esp3, espectro, espectro3, id)
where t2.esp3 like '% y %' and t2.espectro like '%, %';

但不是第二次

三条命名记录如下所示:

Nuestra Señora del Rosario, Santo Domingo y San José

我尝试过这个选项:

select substring(t.shipsnames from '%#",_y#"%' for '#') as name2 from ships t

对#"模式进行了多处更改#"找到空格并获得第二个名字。 然后我尝试了这个选项:

select t2.name2[6:7] from (regexp_split_to_array(t.shipnames, E'\\s+') as name2 from ships t) t2

但它不起作用,因为不是每个记录都有相同的长度,所以有些像{" Santo"," Domingo"}一样被解决,但其他记录不像{&# 34;罗萨里奥"""}。 我对regex sintax不熟悉,我在PostgreSQL文档中找到了这个例子。任何提示?

1 个答案:

答案 0 :(得分:0)

当名称被逗号加上可选空格或 y 分隔时,如果名称必须被强制空格包围,则以下正则表达式将起作用:

\s*,\s*|\s+y\s+

\s:空格字符+:至少一个,*:零或更多,|表示更改。

使用此正则表达式的示例SQL:

SELECT Id, ShipNamesArray[1] ShipName1, ShipNamesArray[2] ShipName2, ShipNamesArray[3] ShipName3
FROM (
    SELECT Id, regexp_split_to_array(Shipnames, '\s*,\s*|\s+y\s+') ShipNamesArray
    FROM (VALUES
        (1, 'Nuestra Señora del Rosario, Santo Domingo y San José'),
        (2, 'Nuestra Señora del Rosario y Santo Domingo'),
        (3, 'Nuestra Señora del Rosario')
    ) AS ExampleShipNames (Id, ShipNames)
) AS SplitShipNames

SQL将生成此输出:

Id | ShipName1                  | ShipName2     | ShipName3
-- | -------------------------- | ------------- | ---------
 1 | Nuestra Señora del Rosario | Santo Domingo | San José
 2 | Nuestra Señora del Rosario | Santo Domingo |
 3 | Nuestra Señora del Rosario |               |