Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7728813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:48:45+00:00 2026-06-01T05:48:45+00:00

I have a feed that is populating a single text field in a table

  • 0

I have a feed that is populating a single text field in a table with statistics.
I need to pull this data into multiple fields in another table
but the strange format makes importing automatically difficult.

The file format is flat text but an example is below:

08:34:52 Checksum=180957248,TicketType=6,InitialUserType=G,InitialUserID=520,CommunicationType=Incoming,Date=26-03-2012,Time=08:35:00,Service=ST,Duration=00:00:14,Cost=0.12

Effectively it’s made up of:

[timestamp] [Field1 name]=[Field1 value],[Field2 name]=[Field2 value],[Field4 name]=[Field4 value]...[CR]

All fields are always in the same order but not always present.
Total columns could be anywhere from 5 to 30.

I’ve tried the below function to translate it which seems to work mostly but seems to randomly skip fields:

Parsing the data:

(SELECT [Data].[dbo].[GetFromTextString] ( 'Checksum=' ,',' ,RAWTEXT)) AS RowCheckSum,
(SELECT [Data].[dbo].[GetFromTextString] ( 'TicketType=' ,',' ,RAWTEXT)) AS TicketType,

And the Function:

CREATE FUNCTION [dbo].[GetFromTextString]
-- Input start and end and return value.
   (@uniqueprefix VARCHAR(100),
    @commonsuffix VARCHAR(100),
    @datastring VARCHAR(MAX) )
RETURNS VARCHAR(MAX) -- Picked Value.
AS
BEGIN

    DECLARE @ADJLEN INT = LEN(@uniqueprefix)

    SET @datastring = @datastring + @commonsuffix

   RETURN ( 
    CASE WHEN (CHARINDEX(@uniqueprefix,@datastring) > 0) 
         AND (CHARINDEX(@uniqueprefix + @commonsuffix,@datastring) = 0)
    THEN SUBSTRING(@datastring, PATINDEX('%' + @uniqueprefix + '%',@datastring)+@ADJLEN, CHARINDEX(@commonsuffix,@datastring,PATINDEX('%' + @uniqueprefix + '%',@datastring))- PATINDEX('%' + @uniqueprefix + '%',@datastring)-@ADJLEN) ELSE NULL END
)
END

Could anyone suggest a better/cleaner way to strip out the data or could someone work out why this formula skips rows?

Any help really appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:48:47+00:00Added an answer on June 1, 2026 at 5:48 am

    NOTE – THE FIRST SOLUTION IS RUBBISH. I HAVE LEFT IN IT FOR HISTORICAL REASONS, BUT A BETTER SOLUTION IS CONTAINED BELOW

    I am not even sure if this will be faster than your current method, but it is the way I would approach the issue (If i was forced into an SQL only solution). The first thing that is required is a table valued function that will perform a split function:

    CREATE FUNCTION dbo.Split (@TextToSplit VARCHAR(MAX), @Delimiter VARCHAR(MAX))
    RETURNS @Values TABLE (Position INT IDENTITY(1, 1) NOT NULL, TextValues VARCHAR(MAX) NOT NULL)
    AS
    BEGIN
        WHILE CHARINDEX(@Delimiter, @TextToSplit) > 0
            BEGIN
                INSERT @Values 
                SELECT  LEFT(@TextToSplit, CHARINDEX(@Delimiter, @TextToSplit) - 1)
                SET @TextToSplit = SUBSTRING(@TextToSplit, CHARINDEX(@Delimiter, @TextToSplit) + 1, LEN(@TextToSplit))
    
            END
            INSERT @Values VALUES (@TextToSplit) 
        RETURN
    END
    

    For my example I am working from a temp table @Worklist, you may need to adapt yours accordingly, or you could just insert the relevant data into @Worklist where I have used dummy data:

    DECLARE @WorkList TABLE (ID INT IDENTITY(1, 1) NOT NULL, TextField VARCHAR(MAX))
    INSERT @WorkList
    SELECT  '08:34:52 Checksum=180957248,TicketType=6,InitialUserType=G,InitialUserID=520,CommunicationType=Incoming,Date=26-03-2012,Time=08:35:00,Service=ST,Duration=00:00:14,Cost=0.12'
    UNION
    SELECT  '08:34:52 Checksum=180957249,TicketType=5,InitialUserType=H,InitialUserID=521,CommunicationType=Outgoing,Date=27-03-2012,Time=14:27:00,Service=ST,Duration=00:15:12,Cost=0.37'
    

    The main bit of the query is done here. It is quite long, so I have tried to comment it as well as possible. If further clarification is required I can add more comments.

    DECLARE @Output TABLE (ID INT IDENTITY(1, 1) NOT NULL, TextField VARCHAR(MAX))
    DECLARE @KeyPairs TABLE (WorkListID INT NOT NULL, KeyField VARCHAR(MAX), ValueField VARCHAR(MAX))
    
    -- STORE TIMESTAMP DATA - THIS ASSUMES THE FIRST SPACE IS THE END OF THE TIMESTAMP
    INSERT @KeyPairs 
    SELECT  ID, 'TimeStamp', LEFT(TextField, CHARINDEX(' ', TextField))
    FROM    @WorkList
    
    -- CLEAR THE TIMESTAMP FROM THE WORKLIST
    UPDATE  @WorkList
    SET     TextField = SUBSTRING(TextField, CHARINDEX(' ', TextField) + 1, LEN(TextField))
    
    DECLARE @ID INT = (SELECT MIN(ID) FROM @WorkList)
    WHILE @ID IS NOT NULL 
        BEGIN
            -- SPLIT THE STRING FIRST INTO ALL THE PAIRS (e.g. Checksum=180957248)
            INSERT @Output
            SELECT  TextValues
            FROM    dbo.Split((SELECT TextField FROM @WorkList WHERE ID = @ID), ',')
    
            DECLARE @ID2 INT = (SELECT MIN(ID) FROM @Output)
    
            -- FOR ALL THE PAIRS SPLIT THEM INTO A KEY AND A VALUE (USING THE POSITION OF THE SPLIT FUNCTION)
            WHILE @ID2 IS NOT NULL
                BEGIN
                    INSERT @KeyPairs
                    SELECT  @ID, 
                            MAX(CASE WHEN Position = 1 THEN TextValues ELSE '' END),
                            MAX(CASE WHEN Position = 2 THEN TextValues ELSE '' END)
                    FROM    dbo.Split((SELECT TextField FROM @Output WHERE ID = @ID2), '=')
    
                    DELETE  @Output
                    WHERE   ID = @ID2
    
                    SET @ID2 = (SELECT MIN(ID) FROM @Output)
                END
    
            DELETE  @WorkList
            WHERE   ID = @ID
    
            SET @ID = (SELECT MIN(ID) FROM @WorkList)
        END
    
    -- WE NOW HAVE A TABLE CONTAINING EAV MODEL STYLE DATA. THIS NEEDS TO BE PIVOTED INTO THE CORRECT FORMAT
    -- ENSURE COLUMNS ARE LISTED IN THE ORDER YOU WANT THEM TO APPEAR
    SELECT  *
    FROM    @KeyPairs p
            PIVOT
            (   MAX(ValueField)
                FOR KeyField IN 
                    (   [TimeStamp], [Checksum], [TicketType], [InitialUserType], 
                        [InitialUserID], [CommunicationType], [Date], [Time],
                        [Service], [Duration], [Cost]
                    )
            ) AS PivotTable;
    

    EDIT (4 YEARS LATER)

    A recent upvote brought this to my attention and the I hate myself a little bit for ever posting this answer in its current form.

    A much better split function would be:

    CREATE FUNCTION dbo.Split
    (
       @List       NVARCHAR(MAX),
       @Delimiter  NVARCHAR(255)
    )
    RETURNS TABLE
    WITH SCHEMABINDING AS
    RETURN
    (   WITH N1 AS (SELECT N FROM (VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1), (1)) n (N)),
        N2(N) AS (SELECT 1 FROM N1 a CROSS JOIN N1 b),
        N3(N) AS (SELECT 1 FROM N2 a CROSS JOIN N2 b),
        N4(N) AS (SELECT 1 FROM N3 a CROSS JOIN N3 b),
        cteTally(N) AS 
        (   SELECT 0 UNION ALL 
            SELECT TOP (DATALENGTH(ISNULL(@List,1))) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) 
            FROM n4
        ),
        cteStart(N1) AS 
        (   SELECT t.N+1 
            FROM cteTally t
            WHERE (SUBSTRING(@List,t.N,1) = @Delimiter OR t.N = 0)
        )
        SELECT Item = SUBSTRING(@List, s.N1, ISNULL(NULLIF(CHARINDEX(@Delimiter,@List,s.N1),0)-s.N1,8000)), 
                Position = s.N1,
                ItemNumber = ROW_NUMBER() OVER(ORDER BY s.N1)
        FROM cteStart s
    );
    

    Then there is no need for looping at all, you just have a proper set based solution by calling the split function twice to get your EAV style data set:

    DECLARE @WorkList TABLE (ID INT IDENTITY(1, 1) NOT NULL, TextField VARCHAR(MAX))
    INSERT @WorkList
    SELECT  '08:34:52 Checksum=180957248,TicketType=6,InitialUserType=G,InitialUserID=520,CommunicationType=Incoming,Date=26-03-2012,Time=08:35:00,Service=ST,Duration=00:00:14,Cost=0.12'
    UNION
    SELECT  '08:34:52 Checksum=180957249,TicketType=5,InitialUserType=H,InitialUserID=521,CommunicationType=Outgoing,Date=27-03-2012,Time=14:27:00,Service=ST,Duration=00:15:12,Cost=0.37';
    
    WITH KeyPairs AS
    (   SELECT  w.ID, 
                [Timestamp] = LEFT(w.TextField, CHARINDEX(' ', w.TextField)),
                KeyField = MAX(CASE WHEN v.ItemNumber = 1 THEN v.Item END),
                ValueField = MAX(CASE WHEN v.ItemNumber = 2 THEN v.Item END)
        FROM    @WorkList AS w
                CROSS APPLY dbo.Split(SUBSTRING(TextField, CHARINDEX(' ', TextField) + 1, LEN(TextField)), ',') AS kp
                CROSS APPLY dbo.Split(kp.Item, '=') AS v
        GROUP BY w.ID, kp.ItemNumber,w.TextField
    )
    SELECT  *
    FROM   KeyPairs AS kp
            PIVOT
            (   MAX(ValueField)
                FOR KeyField IN 
                    (   [Checksum], [TicketType], [InitialUserType], 
                        [InitialUserID], [CommunicationType], [Date], [Time],
                        [Service], [Duration], [Cost]
                    )
            ) AS pvt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an rss feed that I am reading into. I need to retrieve
I have a series of XSL 2.0 stylesheets that feed into each other, i.e.
I have this table: id feed_id ... Let's say that I have 500 rows
I have an Objective C application that reads data from an RSS feed using
I'm pulling data from a feed that I have no control over and I
I have a feed that gives me times (not datetimes) in this format: 20:00:00.000+03:00
I have an XML feed that looks something like this: I can parse the
I have a JSON feed that drupal spits out time in this format: 2010-12-16T04:41:35Z
I have an XML feed that im pulling via javascript and translating it into
I have RSS feed that I want to modify on fly, all I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.