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 8382961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:03:00+00:00 2026-06-09T17:03:00+00:00

I was wondering if I have SQL Server 2008 table that was created like

  • 0

I was wondering if I have SQL Server 2008 table that was created like this:

CREATE TABLE tbl (id INT PRIMARY KEY, 
                  dvt NVARCHAR(32), 
                  d0 TINYINT, 
                  d1 TINYINT, 
                  d2 TINYINT);
INSERT INTO tbl (id, dvt, d0, d1, d2) 
 VALUES(1, '1', NULL, NULL, NULL);
INSERT INTO tbl (id, dvt, d0, d1, d2) 
 VALUES(2, '', NULL, NULL, NULL);
INSERT INTO tbl (id, dvt, d0, d1, d2) 
 VALUES(3, '2,5', NULL, NULL, NULL);
INSERT INTO tbl (id, dvt, d0, d1, d2) 
 VALUES(4, '13, 34, 45, 5', NULL, NULL, NULL);
INSERT INTO tbl (id, dvt, d0, d1, d2) 
 VALUES(5, '1,8, 10', NULL, NULL, NULL);

I need to take the string from the ‘dvt’ column and split it into ‘d0’, ‘d1’ and ‘d2’ columns. The ‘dvt’ value can be separated by commas.

I can do this using C# and a tokenization function but I was wondering if it’s possible to do the same using SQL?

Columns BEFORE:

1, "1",             NULL, NULL, NULL
2, "",              NULL, NULL, NULL
3, "2,5",           NULL, NULL, NULL
4, "13, 34, 45, 5", NULL, NULL, NULL
5, "1,8, 10",       NULL, NULL, NULL

Columns AFTER:

1, "1",             1,    NULL, NULL
2, "",              NULL, NULL, NULL
3, "2,5",           2,    5,    NULL
4, "13, 34, 45, 5", 13,   34,   45  -- 5 is discarded
5, "1,8, 10",       1,    8,    10
  • 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-09T17:03:02+00:00Added an answer on June 9, 2026 at 5:03 pm

    The main problem with this type of code is re-use of calculations.

    SQL Server is good at caching results (If you type the exact same CHARINDEX() caluculation 5 times, it only calculates once and re-uses that result 4 times).

    That’s little consolation for the poor coder who has to type or maintain that code though.

    SQL Server 2005 onward has CROSS APPLY that does help somewhat. The logic is repeated, but the results can be referenced repeatedly, rather that the calculation typed repeatedly.

    SELECT
      *,
      SUBSTRING(dvt, 1,            ISNULL(comma1.pos-1, LEN(dvt))           ) AS item1,
      SUBSTRING(dvt, comma1.pos+1, ISNULL(comma2.pos-1, LEN(dvt))-comma1.pos) AS item2,
      SUBSTRING(dvt, comma2.pos+1, ISNULL(comma3.pos-1, LEN(dvt))-comma2.pos) AS item3
    FROM
    (
      SELECT 'ab,c,def,hij' AS dvt
      UNION ALL
      SELECT 'xyz,abc'      AS dvt
    )
      AS data
    OUTER APPLY
      (SELECT NULLIF(CHARINDEX(',', data.dvt, 1           ), 0) AS pos                     )  AS comma1
    OUTER APPLY
      (SELECT NULLIF(CHARINDEX(',', data.dvt, comma1.pos+1), 0) AS pos WHERE comma1.pos > 0)  AS comma2
    OUTER APPLY
      (SELECT NULLIF(CHARINDEX(',', data.dvt, comma2.pos+1), 0) AS pos WHERE comma2.pos > 0)  AS comma3
    OUTER APPLY
      (SELECT NULLIF(CHARINDEX(',', data.dvt, comma3.pos+1), 0) AS pos WHERE comma3.pos > 0)  AS comma4
    

    Another option is to simply write a table valued user defined function that does this (even when the result of the function is always one row). Then you simply CROSS APPLY that function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008. I have a database table that looks like this
I have this table in a SQL Server 2008 R2 instance which I have
I have a database table (SQL Server 2008 R2 Express edition) that has a
I have a table that contains a GEOMETRY data type. SQL Server 2008 ships
I also have a very large table in SQL Server (2008 R2 Developer Edition)
I have around 20 jobs running in SQL Server 2008. The owner of this
I have a application that uses LocalDataCache to synchronise a SQL Server 2008 Express
Alright so a quick SQL question here(using sql-server-2008). I have a mapping table names
SQL Server 2008 I have a query with several local variables that does some
Is there an inherent cost to using inline-table-valued functions in SQL Server 2008 that

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.