Thursday, January 26, 2017

Sql Server - Split single row in multiple rows based on range

CREATE TABLE Table1 (id int, name varchar(10), start int, [end] int);
GO

INSERT INTO Table1 Values
(13,'sub1', 11, 15),
(15,'sub2', 17, 22),
(19,'sub3', 44, 44);



;WITH CTE 
AS 
(
  select id, name, start, [end] from Table1
  union all
  select id, name, start+1, [end]
  from CTE where [end] > start
)
select id, name, start as 'serial' From CTE order by start option (maxrecursion 0)



idnameserial
13sub111
13sub112
13sub113
13sub114
13sub115
15sub217
15sub218
15sub219
15sub220
15sub221
15sub222
19sub344

No comments:

Post a Comment

Encrypt/Decrypt the App.Config

Program.cs using System; using System.Diagnostics; using System.IO; namespace EncryptAppConfig {     internal class Program     {         pr...