私はこの質問に苦労してこのフォーラムにある解決策を試していますが、私は続行できません。私は助けが必要です。 私は、船の寿命を超えて船名を格納する列を持っています。私はそれらを3つの列に分割したいと思います。 主に私はこれらの3つのオプション、PostgreSQLサブストリングのスペースが空白
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)3つの名前を持っている:私は最初
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 '%, %';
を得ることができます
2番目以外
レコードは次のようになり名付け3:空白を見つけて、2番目の名前を取得するには#「パターン#」にはいくつかの変更により
select substring(t.shipsnames from '%#",_y#"%' for '#') as name2 from ships t
:私は、このオプションを試してみました
Nuestra Señora del Rosario, Santo Domingo y San José
。
select t2.name2[6:7] from (regexp_split_to_array(t.shipnames, E'\\s+') as name2 from ships t) t2
ないすべてのレコードは、ので、いくつかは、{「サント」、「ドミンゴ」}が、他のではない好き{「ロザリオ」のように解決されているのと同じ長さを持っているので、しかし、それは動作しません: は、それから私は、このオプションを試してみました、 "、"} 私はregex sintaxに慣れていません。この例はPostgreSQLのドキュメントにあります。何かヒント?
\s*,\s*|\s+y\s+
\s
:空白文字、+
彼らはコンマプラスオプションの空白または次の正規表現がうまくいく必須空白に囲まYで分離されるたびに名前を分割すべき
が ' '[\\よ、] +'' Eを試してみてください空白とコンマの両方。 –
正確に何をしたいですか? 「NuestraSeñoradel Rosario、Santo Domingo y SanJosé」が入力された場合、「NuestraSeñoradel Rosario」、「Santo Domingo」、「SanJosé」を出力しますか?はいの場合は、3つの別々の列を持つ1つの行、または1つの列を持つ3つの行? a)とb)のソリューションを例として提供できますか? –
@ markus-benko私が使用したレコードの部分文字列にコードを追加しました。それは正常に働いた。フィードバックありがとう –