简体   繁体   中英

SQL query, extracting words/ information from a column of Data

Is there a sql code that takes text from after the first arrow, but stops at a second? (if applicable). Using dbms PostgreSQL.

eg extracting the information in bold

Cat -->  
Park Bench -->  
Test -->  
Train Journey -->  -->  ha 64,10 cm (19 Woll)
Elaborate -->  
HUH -->  --> Yellow 122
HUH -->  --> Yellow 123
HUH -->  --> Yellow 124
HUH -->  --> Yellow 125
HUH -->  
Branches -->  
Branches -->  
Branches -->  
Two way -->  -->  ha 33,02 cm (13 Woll)
Ordinary -->  
Two way -->  -->  ha 49,18 cm (17 Woll)
Two way -->  / Lewisham
Two way -->  -->   ha 33,02 cm (13 Woll)
Ordinary -->  --> Scarf
Ordinary -->  
Window / Door / House -->  --> House - Park
Two way-->  --> Scarf
Two way-->  
HUH -->  --> Spoon 123
HUH -->  --> Stage
Production -->  --> Now
Window / Door -->  --> Now
Window / Door -->  
Window / Door -->  --> Sonstige
Ordinary -->  
HUH -->  
Window / Door -->  -->  ha 45,72 cm (18 Woll)
Ordinary -->  --> Scarf

You can use the split_part() function to get that element:

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

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

You can use some split function with delimiter '-->', below is a split function I use in 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

You should specify the index which in your case is 1

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM