简体   繁体   中英

Extracting Specific words from a dynamic string source

Hello I'm new to the Stack overflow though I've been using this site to search for answers for years. So I have this problem to extract various and specific details from a very LONG string. Problem is, the Strings are not static and doesn't have a fixed index so I can't use the substring function. Example of a string output is like this.

Sample 1:
1 AJ 552 O 9/28/2016 0:0:0 6 PSI KTL 0   GK 2 9/28/2016 7:20:0 9/28/2016 8:35:0 O20YEARS 32.2400

Sample 2:
1 AJ 2552 O 10/8/2016 0:0:0 6 PSI KTL 0   GK 2 10/28/2016 7:0:0 9/28/2016 8:5:0 O20YEARS 32.2400

As you can see, indexes of the strings are adjusted accordingly thus I can't rely on a fixed substring function.

I'm fairly new to SQL and would like your advice on how should I approach this.

So far, methods i tried are only substrings.

UPDATE:

Sorry, I forgot to mention that I'm not extracting data base on any characters or string. I'm extracting an exact part from this string. For instance,

Sample 1:
1 AJ 552 O 9/28/2016 0:0:0 6 PSI KTL 0   GK 2 9/28/2016 7:20:0 9/28/2016 8:35:0 O20YEARS 32.2400

I need to extract the First Date which is "9/28/2016" and the "PSI KTL"

So technically, I need to extract a certain nth word from a string.

Right now, I'm trying to use the space as a delimiter and assigning each word into temp table but it seems it is not Working as of now.

Thanks!

Try this:

CREATE FUNCTION GetWordBasedOnIndex(@text varchar(max),@index int)
RETURNS varchar(50)
AS
BEGIN
    DECLARE @RET VARCHAR(50),@counter int
    SET @text = LTRIM(RTRIM(@text))
    SET @counter = 1
    IF @counter < @index
    BEGIN
            SET @text = LTRIM(RTRIM(SUBSTRING(@text, CHARINDEX(' ',@text,1), LEN(@text) - CHARINDEX(' ',@text,1)+1)))
            SET @text = dbo.GetWordBasedOnIndex(@text,@index-1)
    END
    IF CHARINDEX(' ',@text,1)=0 SET  @RET = @text
    ELSE SET  @RET = SUBSTRING(@text,@counter,CHARINDEX(' ',@text,1))
    RETURN @RET
END

declare @string nvarchar(max)
set @string = '1 AJ 552 O 9/28/2016 0:0:0 6 PSI KTL 0 GK 2 9/28/2016 7:20:0 9/28/2016 8:35:0 O20YEARS 32.2400';
SELECT DBO.GetWordBasedOnIndex(@string, 5)

In case passing from a table and take word based on index:

declare @string nvarchar(max)
set @string = '1 AJ 552 O 9/28/2016 0:0:0 6 PSI KTL 0 GK 2 9/28/2016 7:20:0 9/28/2016 8:35:0 O20YEARS 32.2400';
SELECT [dbo].[GetWordBasedOnIndex](@string, 5)


CREATE table #temp (msg nvarchar(max))
INSERT INTO #temp VALUES(@string)
SELECT [dbo].[GetWordBasedOnIndex](msg, 5) as test from #temp

drop table #temp

please try this:

DECLARE @s varchar(8000)
DECLARE @s2 varchar(8000)


set @s = '1 AJ 552 O 9/28/2016 0:0:0 6 PSI KTL 0   GK 2 9/28/2016 7:20:0 9/28/2016 8:35:0 O20YEARS 32.2400';

SET @s2 =SUBSTRING(@s, CHARINDEX('PSI KTL', @s), 8000)

select SUBSTRing(@s2, PATINDEX('%__/__/____%', @s2), 10)

UPDATE:

if you are looking to parse the string using space as delimiter, you can use the String_Split of SQL 2016 or similar UDF function for other versions:

DECLARE @s varchar(max)

set @s = '1 AJ 2552 O 10/8/2016 0:0:0 6 PSI KTL 0   GK   2 10/28/2016 7:0:0 9/28/2016 8:5:0 O20YEARS 32.2400';
SET @s = replace(@s, '  ', ' '); -- Here, you can replace other white spaces by single space.

WITH cte 
AS (
        SELECT  *,
                ROW_NUMBER() Over(ORDER BY (SELECT null)) as RN
        from string_split(@s, ' ')
    )
        SELECT * FROM cte 
         WHERE RN = 15

Another simple solution , if you using SQL Server 2016

1) Use STRING_SPLIT function that Splits the character expression using specified separator.

3) Detect the date values via using ISDATE function

2) Finally use Top 1 for retriving the first row.

As the following:-

SELECT TOP 1 * 
FROM STRING_SPLIT ('1 AJ 552 O 9/28/2016 0:0:0 6 PSI KTL 0   GK 2 9/28/2016 7:20:0 9/28/2016 8:35:0 O20YEARS 32.2400
', ' ') 
WHERE ISDATE(value) = 1 

Result:

value
9/28/2016

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