Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to find the skip no between StartNo and EndNo

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-23 : 11:24:26
Hello Guys,

I have to find the skip numbers between the following two fields (StartNo and EndNo) than update thoes skip no on SkipNo fields by seperating any character. On the following first data, the skipno are : ABCD00010002;ABCD00010003;ABCD00010004

Declare @tbl1 table
(ID INT,
StartNo varchar(50),
EndNo varchar(50),
SkipNo varchar(255)
)

INSERT INTO @tbl1
SELECT 1, 'ABCD00010001','ABCD00010005',NULL

INSERT INTO @tbl1
SELECT 2, 'ABCD00010006','ABCD00010015',NULL

INSERT INTO @tbl1
SELECT 3, 'ABCD00010050','ABCD00010101',NULL

INSERT INTO @tbl1
SELECT 4, 'ABCD00010500','ABCD00010600',NULL
select * from @tbl1

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-23 : 11:33:45
Are there fixed rules to extract the number part of that varchar column?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-23 : 11:37:35
Yes, the first 4 are alphabet it could be XXXX or MNOP some like that and rest 8 digit are number
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-23 : 11:41:39
update @tbl1
set SkipNo=convert(int,right(EndNo,8)) - convert(int,right(StartNo,8))


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-23 : 11:46:43
I need thoes skipping numbers.
Like, if i have StartNo is 'ABCD00010001' and EndNo is 'ABCD00010005'
than the SkipNo like this :
ABCD00010002;ABCD00010003;ABCD00010004;
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-06-23 : 12:02:37
Oh sorry.
I'm not the right man to something like that in a relational database.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-23 : 13:20:51
Anybody have great idea?
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-06-23 : 14:30:30
Could you do with a list of the missing numbers?
'ABCD00010500','ABCD00010600' will produce a list of 98 "NNNNXXXXXXXX' and 97 commas to separate them.

Jim
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-06-23 : 15:12:58
Don't do anything with this code. Peso or Visakh will come up with something much smarter within minutes of me posting! Probably done in about 20 lines or less.

Jim


Declare @tbl1 table
(ID INT,
StartNo varchar(50),
EndNo varchar(50),
SkipNo varchar(255)
)

INSERT INTO @tbl1
SELECT 1, 'ABCD00010001','ABCD00010005',NULL

INSERT INTO @tbl1
SELECT 2, 'EFGH00010006','ABCD00010015',NULL

--INSERT INTO @tbl1
--SELECT 3, 'ABCD00010050','ABCD00010101',NULL
--
--INSERT INTO @tbl1
--SELECT 4, 'ABCD00010500','ABCD00010600',NULL

DECLARE @Table TABLE (id int,skipno char(12))

declare @skipno char(12)
declare @skipdiff int
declare @ctr int
declare @root int
declare @prefix char(4)
declare @id int

set @id = 1


WHILE @id < 5
BEGIN
SET @ctr = 1
SET @skipdiff = (select convert(int,right(endno,8)) - convert(int,right(startno,8))
from @tbl1
where id = @id)
SET @prefix = (select left(startno,4)
from @tbl1
where id = @id)
SET @root = (select convert(int,right(startno,8))
from @tbl1
where id = @id)
WHILE @ctr < @skipdiff
BEGIN
--select @ctr,@skipdiff,@prefix,@root,@skipno
set @skipno = @prefix + RIGHT('00000000'+convert(varchar(8),@root+@ctr) ,8)

set @ctr = @ctr + 1
INSERT INTO @Table SELECT @id,@Skipno

END --@ctr < @skipdiff


SET @id = @id + 1
END --@id < 5


select distinct
t1.id
,[List] = substring(
( select ', ' + skipno-- as [SkipNumber]
from @table t2
where t2.id = t1.id
for xml path(''), elements ),2,8000)

from @table t1

Go to Top of Page

eonmantra
Starting Member

11 Posts

Posted - 2009-06-23 : 15:41:22
I think JimF has already answered your question, but reading your post I could not help but wonder if your StartNo and EndNo fields are pre-populated. In other words are you having to do this to an existing table that is already populated? If not and you are having to populate all the data you might turn this into a function that takes a StartNo and EndNo input and automatically inserts those fields, while also calculating the SkipNo field and then inserting it as well.

Just a thought.

Edit:

Here is an alternative method for updating your table.

SET NOCOUNT ON;
DECLARE @i AS INT;
SET @i = (SELECT MAX(ID) FROM @tbl1);
DECLARE @min AS INT;
SET @min = (SELECT MIN(ID) FROM @tbl1);
-- update the SkipNo column to '' instead of NULL to work with code later on...
UPDATE @tbl1 SET SkipNo = '';
WHILE @i >= @min
BEGIN
IF (SELECT CONVERT(INT,RIGHT(EndNo,8)) - CONVERT(INT,RIGHT(StartNo,8)) FROM @tbl1 WHERE ID = @i) IS NOT NULL

BEGIN
DECLARE @ii AS INT;
SET @ii = (SELECT MAX(CONVERT(INT,RIGHT(EndNo,8)) - CONVERT(INT,RIGHT(StartNo,8))-1) FROM @tbl1 WHERE ID = @i);
WHILE @ii > 0
BEGIN
UPDATE @tbl1
SET SkipNo = LEFT(StartNo,4)+RIGHT('00000000'+CONVERT(varchar,(convert(int,right(StartNo,8) + @ii))),8) + ';' + SkipNo
WHERE ID = @i;
SET @ii = @ii - 1;
END
SET @i = @i - 1;
END

ELSE
SET @i = @i - 1;
END

Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-06-24 : 10:10:38
Thanks guys,
Perfect job.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-24 : 10:25:41
quote:
Originally posted by jimf

Probably done in about 20 lines or less.
DECLARE	@Sample TABLE 
(
ID INT,
StartNo CHAR(12),
EndNo CHAR(12),
SkipNo VARCHAR(MAX)
)

INSERT @Sample
SELECT 1, 'ABCD00010001', 'ABCD00010005', NULL UNION ALL
SELECT 2, 'ABCD00010006', 'ABCD00010015', NULL UNION ALL
SELECT 3, 'ABCD00010050', 'ABCD00010101', NULL UNION ALL
SELECT 4, 'ABCD00010500', 'ABCD00010600', NULL

SELECT s.ID,
s.StartNo,
s.EndNo,
f.g AS SkipNo
FROM @Sample AS s
CROSS APPLY (
SELECT LEFT(s.StartNo, 4) + REPLACE(STR(1 * RIGHT(s.StartNo, 8) + v.Number, 8), ' ', '0') + ';'
FROM master..spt_values AS v
WHERE v.Type = 'P'
AND v.Number >= 1
AND v.Number < 1 * RIGHT(s.EndNo, 8) - 1 * RIGHT(s.StartNo, 8)
FOR XML PATH('')
) AS f(g)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-06-24 : 10:26:05
Geez, took you long enough!

Jim
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-24 : 10:31:41
Another attempt since @Sample has to be scanned anyway due to missing filtering
SELECT		s.ID,
s.StartNo,
s.EndNo,
s.Items,
s.Intro,
f.g AS SkipNo
FROM (
SELECT ID,
StartNo,
EndNo,
LEFT(StartNo, 4) AS Prefix,
1 * RIGHT(StartNo, 8) AS Intro,
1 * RIGHT(EndNo, 8) - 1 * RIGHT(StartNo, 8) - 1 AS Items
FROM @Sample
) AS s
CROSS APPLY (
SELECT s.Prefix + REPLACE(STR(s.Intro + v.Number + 1, 8), ' ', '0') + ';'
FROM master..spt_values AS v
WHERE v.Type = 'P'
AND v.Number < s.Items
FOR XML PATH('')
) AS f(g)



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-24 : 10:32:10
quote:
Originally posted by jimf

Geez, took you long enough!
I'm busy!




E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -