I have created a fiddle here for the below data and for the query I have tried.
I have table like below.
My expected output is as below
Logic:
I have to create the report as shown above. The logic is to repeat the same rows from the starting number to ending material number. Find the difference and if there is a difference repeat the rows for each material number.All other column value remains same.
Data format.
- MaterialNo will have only one hyphen.(varchar type)
- The number of characters before and after the hyphen may vary.
- The difference may go upto 150.
So what i have tried
WITH cte
AS (
SELECT Materialno_start,Materialno_end,name,mtype,noofstock
,starts.st AS ns,ends.ed AS nd,diff.s AS d,i = 1
,n = convert(VARCHAR(30), starts.st)
,n.base AS bs
FROM data
CROSS APPLY (VALUES (len(Materialno_start))) leng(mn)
CROSS APPLY (VALUES (charindex('-', Materialno_start)) ) s(hyp)
CROSS APPLY (VALUES (substring(Materialno_start, s.hyp - leng.mn + 1, leng.mn))) n(base)
CROSS APPLY (VALUES (substring(Materialno_start, s.hyp + 1, leng.mn)) ) starts(st)
CROSS APPLY (VALUES (substring(coalesce(Materialno_end, Materialno_start), s.hyp + 1, leng.mn)) ) ends(ed)
CROSS APPLY (VALUES (convert(INT, ends.ed) - convert(INT, starts.st))) diff(s)
UNION ALL
SELECT Materialno_start,Materialno_end,name,mtype,noofstock ,ns,nd,d,i = i + 1
,n = convert(VARCHAR(30), n + 1)
,bs
FROM cte
WHERE i <= d
)
SELECT Materialno_start
,Materialno_end
,bs + n AS MaterialNo
,Name
,mtype
,noofstock
FROM cte
ORDER BY 1
It is giving me the required output. But i'm not sure if it is efficient as there are so many CROSS APPLY and my production data may have around 70k rows and it may go upto 120k rows after splitting. I would like to know if this can be done in any other way more efficiently or what are the things I can improve in this query. I don't have access to production data or QA.So i cannot test this in real data. I was given sample data of 100 rows and I used this query to achieve my output.

