0

I have a string/text element that I need to split and insert into a table.

I have the text values of

123456|House,123567|Flat,789463|Car,635491|Lorry,

Where the number value (nvalue) needs to go in the first column, and the type eg. House needs to be in the second.

The values before the "," need to be on the same row. I would be looking for a table resembling

╔══════════╦═══════════╗
║ nvalue   ║ Type      ║
╠══════════╬═══════════╣
║   123456 ║     House ║
║   123567 ║      Flat ║
║   789463 ║       Car ║
║   635491 ║     Lorry ║
╚══════════╩═══════════╝

I'm trying to use the SQL code

INSERT INTO TABLE resultsTable
SELECT 
nvalue({status}, ';')[255],
type({status}| ';')[255],

but i'm not having any luck.

{status} is a text field where the string is located.

1
  • SQL Server 2016 has a split function to do this. Commented Jun 26, 2017 at 11:45

3 Answers 3

1

In SQL Server 2016+ you can use string_split().

In SQL Server pre-2016, using a CSV Splitter table valued function by Jeff Moden along with left() and stuff() (or right()) with charindex():

declare @status nvarchar(max) = '123456|House,123567|Flat,789463|Car,635491|Lorry,'

select 
    nvalue = left(s.Item,charindex('|',s.Item)-1)
  , [Type] = stuff(s.Item,1,charindex('|',s.Item),'')
from dbo.DelimitedSplitN4K(@status,',') s
where s.Item <>''

rextester demo: http://rextester.com/QQZUC78477

returns:

+--------+-------+
| nvalue | Type  |
+--------+-------+
| 123456 | House |
| 123567 | Flat  |
| 789463 | Car   |
| 635491 | Lorry |
+--------+-------+

splitting strings reference:

Sign up to request clarification or add additional context in comments.

Comments

0

You can either use split() from the web or in SQL Server 2016. Then:

with lines(line) as (
      select l.*
      from dbo.split(@str, '|') l
     )
insert into resultsTable (nvalue, type)
    select left(line, charindex(',', line) - 1),
           stuff(line, charindex(',', line), len(line), '')
    from lines;

Comments

0

Using SUBSTRING(), Xml and Cross apply we can get desired result

DECLARE @Value NVARCHAR(max) = ',123456|House,123567|Flat,789463|Car,635491|Lorry,'
DECLARE @DYVALUE TABLE (VALUE NVARCHAR(MAX))

INSERT INTO @DYVALUE (VALUE)
SELECT @VALUE

;WITH cte
AS (
    SELECT Split.a.value('.', 'VARCHAR(1000)') AS Value
    FROM (
        SELECT CAST('<S>' + REPLACE(Value, ',', '</S><S>') + '</S>' AS XML) AS Value
        FROM @DyValue
        ) AS A
    CROSS APPLY Value.nodes('/S') AS Split(a)
    )
SELECT *
FROM (
    SELECT SUBSTRING(Value, 0, CHARINDEX('|', Value)) AS nvalue
        ,SUBSTRING(Value, CHARINDEX('|', Value) + 1, LEN(Value)) AS [Type]
    FROM Cte
    ) DT
WHERE nvalue <> ''
    AND [Type] <> ''

Result:

+--------+-------+
| nvalue | Type  |
+--------+-------+
| 123456 | House |
| 123567 | Flat  |
| 789463 | Car   |
| 635491 | Lorry |
+--------+-------+

2 Comments

I'm using SQL server 2012 (sorry should have mentioned that), but this works really well. The only issue is i'm having trouble adding the values to database resultsTable. Do you know how I can take the result and put it into a specific SQL table. I've tried Insert INTO resultsTable (nvalue, type) Value (nvalue, [type]) but it just shoots out an error
I managed to get this working by adding "INSERT INTO resultsTable" Changing the code to look like "CROSS APPLY Value.nodes('/S') AS Split(a) ) insert into Missed_Collective_Events SELECT * FROM ("

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.