title: Can I tokenize a string using t-SQL
body: <p>I was wondering if I have SQL Server 2008 table that was created like this:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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.</p>
  
  <p>I can do this using C# and a tokenization function but I was wondering if it's possible to do the same using SQL?</p>
  
  <p>Columns BEFORE:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Columns AFTER:</p>
  
  <pre><code>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
  </code></pre>