SQL查询,从一列Data中提取单词/信息

时间:2016-03-29 08:24:18

标签: sql postgresql

是否有一个sql代码从第一个箭头后面获取文本,但是在一秒钟后停止? (如适用)。使用dbms PostgreSQL。

例如以粗体

提取信息
Cat --> Dog 
Park Bench --> Window / Door / House 
Test --> OBJECT 
Train Journey --> Coat -->  ha 64,10 cm (19 Woll)
Elaborate --> All-in-One 
HUH --> Scarf --> Yellow 122
HUH --> Scarf --> Yellow 123
HUH --> Scarf --> Yellow 124
HUH --> Scarf --> Yellow 125
HUH --> Scarf 
Branches --> Wood / Leaf 
Branches --> Scarf 
Branches --> Car 
Two way --> Drawers -->  ha 33,02 cm (13 Woll)
Ordinary --> Table 
Two way --> Drawers -->  ha 49,18 cm (17 Woll)
Two way --> 5-in-2 / Lewisham
Two way --> Drawers -->   ha 33,02 cm (13 Woll)
Ordinary --> London --> Scarf
Ordinary --> ThinHouse 
Window / Door / House --> Hotel --> House - Park
Two way--> London --> Scarf
Two way--> London 
HUH --> Bowl --> Spoon 123
HUH --> Hand --> Stage
Production --> Able --> Now
Window / Door --> Always --> Now
Window / Door --> London 
Window / Door --> Longevity --> Sonstige
Ordinary --> London 
HUH --> HUH Window / Door 
Window / Door --> Longevity -->  ha 45,72 cm (18 Woll)
Ordinary --> London --> Scarf

2 个答案:

答案 0 :(得分:1)

您可以使用split_part()函数获取该元素:

select split_part(the_column, '-->', 2)
from your_table;

SQLFiddle示例:http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/7123

答案 1 :(得分:0)

你可以使用一些分隔函数和分隔符' - >',下面是我在t-sql中使用的分割函数:

CREATE FUNCTION [dbo].[Split]
(
    @Text VARCHAR(MAX),
    @Delimiter VARCHAR(100),
    @Index INT
)
RETURNS VARCHAR(MAX)
AS BEGIN
    DECLARE @A TABLE (ID INT IDENTITY, V VARCHAR(MAX));
    DECLARE @R VARCHAR(MAX);
    WITH CTE AS
    (
    SELECT 0 A, 1 B
    UNION ALL
    SELECT B, CONVERT(INT,CHARINDEX(@Delimiter, @Text, B) + LEN(@Delimiter))
    FROM CTE
    WHERE B > A
    )
    INSERT @A(V)
    SELECT SUBSTRING(@Text,A,CASE WHEN B > LEN(@Delimiter) THEN B-A-LEN(@Delimiter) ELSE LEN(@Text) - A + 1 END) VALUE      
    FROM CTE WHERE A >0

    SELECT      @R
    =           V
    FROM        @A
    WHERE       ID = @Index + 1
    RETURN      @R
END
GO

您应该指定在您的情况下为1的索引

SELECT SPLIT(myField,'-->',1) FROM myTable