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

  • SEARCH
  • Home
  • 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 5841239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:51:52+00:00 2026-05-22T11:51:52+00:00

I have a SQL table with column which contain string like ‘type|type1|type2|type3|type4’. I need

  • 0

I have a SQL table with column which contain string like ‘type|type1|type2|type3|type4’.
I need to select the string, id. Split string and insert to another table. First item should be default and I need to get identity of it and insert to another table with the type value.

Please help me to create T-SQL query to accomplish desirable result.

Example

Step 1 To select Items from Table 1.
Step 2 Split to array
Step 3 Insert to Table 2 (first item will be default)
Step 4 Update Table 1 with default Type Value based on TypeID and Default True

Table 1

ID       Items                           Default
--------------------------------------------------
1        type|type1|type2|type3|type4
2        type|type1|type2|type3|type4

Table 2

ID   TypeID        Type    Default(bool)
--------------------------------------------------
1    1             type1   1
2    1             type2   0
  • 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-05-22T11:51:52+00:00Added an answer on May 22, 2026 at 11:51 am

    Using the split function from Arnold Fribble’s answer on this thread

    Create FUNCTION dbo.Split (@sep char(1), @s varchar(512))
    RETURNS table
    AS
    RETURN (
        WITH Pieces(pn, start, stop) AS (
          SELECT 1, 1, CHARINDEX(@sep, @s)
          UNION ALL
          SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
          FROM Pieces
          WHERE stop > 0
        )
        SELECT pn,
          SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
        FROM Pieces
      )
    GO
    

    You can write the following (I made some guesses about the ID field in table2 and what the defaultTypeID should be in table1 but you should be able to adjust that)

    CREATE TABLE #table1       ( 
         id            INT, 
         items         VARCHAR(MAX), 
         defaulttypeid INT 
      ) 
    
    CREATE TABLE #table2       ( 
         id        INT IDENTITY, 
         typeid    INT, 
         TYPE      VARCHAR(5), 
         isdefault BIT 
      ) 
    
    INSERT INTO #table1 
    VALUES      (1, 
                 'type|type1|type2|type3|type4', 
                 NULL), 
                (2, 
                 'type|type1|type2|type3|type4', 
                 NULL) 
    
    
    INSERT INTO #table2 
                (typeid, 
                 TYPE, 
                 isdefault) 
    SELECT id             typeid, 
           Rtrim(split.s) AS item, 
           CASE 
             WHEN ( split.pn = 1 ) THEN 1 
             ELSE 0 
           END            AS isdefault 
    FROM   #table1 
           CROSS APPLY test.dbo.Split('|', items) AS split 
    
    UPDATE #table1 
    SET    defaulttypeid = t2.ID
    FROM   #table1 t1 
           INNER JOIN #table2 t2 
             ON t1.id = t2.typeid 
                AND t2.isdefault = 1 
    
    
    DROP TABLE #table1 
    
    DROP TABLE #table2 
    

    This outputs

    ID          Items                          DefaultTypeID
    ----------- ------------------------------ -------------
    1           type|type1|type2|type3|type4    1     
    2           type|type1|type2|type3|type4    6
    
    
    
    ID          TypeID      Type  IsDefault
    ----------- ----------- ----- ---------
    1           1           type  1
    2           1           type1 0
    3           1           type2 0
    4           1           type3 0
    5           1           type4 0
    6           2           type  1
    7           2           type1 0
    8           2           type2 0
    9           2           type3 0
    10          2           type4 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single column table which looks like this: Gaming | Austria |
I have a table in SQL Server 2005 which has three columns: id (int),
MS SQL Server 2000 I have a column in Table A called Name. I
I have a nullable DateTime column in my SQL Server 2005 table called DateTimeDeleted.
I have a table (T1) in t-sql with a column (C1) that contains almost
I am using SQL Server 2005. I have a table with a text column
I have a SQL table with news stories and Unix timestamps. I'd like to
I have a SQL table which has a number of fields ID | Value
I have a SQL table like so: Update: I'm changing the example table as
I need to check If a column value (string) in SQL server table starts

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.