SQL query to find Missing sequence numbers - sql-server-2005
I have a column named sequence. The data in this column looks like 1, 2, 3, 4, 5, 7, 9, 10, 15.
I need to find the missing sequence numbers from the table. What SQL query will find the missing sequence numbers from my table? I am expecting results like
Missing numbers
---------------
6
8
11
12
13
14
I am using only one table. I tried the query below, but am not getting the results I want.
select de.sequence + 1 as sequence from dataentry as de
left outer join dataentry as de1 on de.sequence + 1 = de1.sequence
where de1.sequence is null order by sequence asc;
How about something like:
select (select isnull(max(val)+1,1) from mydata where val < md.val) as [from],
md.val - 1 as [to]
from mydata md
where md.val != 1 and not exists (
select 1 from mydata md2 where md2.val = md.val - 1)
giving summarised results:
from to
----------- -----------
6 6
8 8
11 14
I know this is a very old post but I wanted to add this solution that I found HERE so that I can find it easier:
WITH Missing (missnum, maxid)
AS
(
SELECT 1 AS missnum, (select max(id) from #TT)
UNION ALL
SELECT missnum + 1, maxid FROM Missing
WHERE missnum < maxid
)
SELECT missnum
FROM Missing
LEFT OUTER JOIN #TT tt on tt.id = Missing.missnum
WHERE tt.id is NULL
OPTION (MAXRECURSION 0);
Try with this:
declare #min int
declare #max int
select #min = min(seq_field), #max = max(seq_field) from [Table]
create table #tmp (Field_No int)
while #min <= #max
begin
if not exists (select * from [Table] where seq_field = #min)
insert into #tmp (Field_No) values (#min)
set #min = #min + 1
end
select * from #tmp
drop table #tmp
The best solutions are those that use a temporary table with the sequence. Assuming you build such a table, LEFT JOIN with NULL check should do the job:
SELECT #sequence.value
FROM #sequence
LEFT JOIN MyTable ON #sequence.value = MyTable.value
WHERE MyTable.value IS NULL
But if you have to repeat this operation often (and more then for 1 sequence in the database), I would create a "static-data" table and have a script to populate it to the MAX(value) of all the tables you need.
SELECT CASE WHEN MAX(column_name) = COUNT(*)
THEN CAST(NULL AS INTEGER)
-- THEN MAX(column_name) + 1 as other option
WHEN MIN(column_name) > 1
THEN 1
WHEN MAX(column_name) <> COUNT(*)
THEN (SELECT MIN(column_name)+1
FROM table_name
WHERE (column_name+ 1)
NOT IN (SELECT column_name FROM table_name))
ELSE NULL END
FROM table_name;
Here is a script to create a stored procedure that returns missing sequential numbers for a given date range.
CREATE PROCEDURE dbo.ddc_RolledBackOrders
-- Add the parameters for the stored procedure here
#StartDate DATETIME ,
#EndDate DATETIME
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Min BIGINT
DECLARE #Max BIGINT
DECLARE #i BIGINT
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
BEGIN
DROP TABLE #TempTable
END
CREATE TABLE #TempTable
(
TempOrderNumber BIGINT
)
SELECT #Min = ( SELECT MIN(ordernumber)
FROM dbo.Orders WITH ( NOLOCK )
WHERE OrderDate BETWEEN #StartDate AND #EndDate)
SELECT #Max = ( SELECT MAX(ordernumber)
FROM dbo.Orders WITH ( NOLOCK )
WHERE OrderDate BETWEEN #StartDate AND #EndDate)
SELECT #i = #Min
WHILE #i <= #Max
BEGIN
INSERT INTO #TempTable
SELECT #i
SELECT #i = #i + 1
END
SELECT TempOrderNumber
FROM #TempTable
LEFT JOIN dbo.orders o WITH ( NOLOCK ) ON tempordernumber = o.OrderNumber
WHERE o.OrderNumber IS NULL
END
GO
Aren't all given solutions way too complex?
wouldn't this be much simpler:
SELECT *
FROM (SELECT row_number() over(order by number) as N from master..spt_values) t
where N not in (select 1 as sequence union
select 2 union
select 3 union
select 4 union
select 5 union
select 7 union
select 10 union
select 15
)
This is my interpretation of this issue, placing the contents in a Table variable that I can easily access in the remainder of my script.
DECLARE #IDS TABLE (row int, ID int)
INSERT INTO #IDS
select ROW_NUMBER() OVER (ORDER BY x.[Referred_ID]), x.[Referred_ID] FROM
(SELECT b.[Referred_ID] + 1 [Referred_ID]
FROM [catalog].[dbo].[Referrals] b) as x
LEFT JOIN [catalog].[dbo].[Referrals] a ON x.[Referred_ID] = a.[Referred_ID]
WHERE a.[Referred_ID] IS NULL
select * from #IDS
Just for fun, I decided to post my solution.
I had an identity column in my table and I wanted to find missing invoice numbers.
I reviewed all the examples I could find but they were not elegant enough.
CREATE VIEW EENSkippedInvoicveNo
AS
SELECT CASE WHEN MSCNT = 1 THEN CAST(MSFIRST AS VARCHAR (8)) ELSE
CAST(MSFIRST AS VARCHAR (8)) + ' - ' + CAST(MSlAST AS VARCHAR (8)) END AS MISSING,
MSCNT, INV_DT FROM (
select invNo+1 as Msfirst, inv_no -1 as Mslast, inv_no - invno -1 as msCnt, dbo.fmtdt(Inv_dt) AS INV_dT
from (select inv_no as invNo, a4glidentity + 1 as a4glid
from oehdrhst_sql where inv_dt > 20140401) as s
inner Join oehdrhst_sql as h
on a4glid = a4glidentity
where inv_no - invno <> 1
) AS SS
DECLARE #MaxID INT = (SELECT MAX(timerecordid) FROM dbo.TimeRecord)
SELECT SeqID AS MissingSeqID
FROM (SELECT ROW_NUMBER() OVER (ORDER BY column_id) SeqID from sys.columns) LkUp
LEFT JOIN dbo.TimeRecord t ON t.timeRecordId = LkUp.SeqID
WHERE t.timeRecordId is null and SeqID < #MaxID
I found this answer here:
http://sql-developers.blogspot.com/2012/10/how-to-find-missing-identitysequence.html
I was looking for a solution and found many answers. This is the one I used and it worked very well. I hope this helps anyone looking for a similar answer.
-- This will return better Results
-- ----------------------------------
;With CTERange
As (
select (select isnull(max(ArchiveID)+1,1) from tblArchives where ArchiveID < md.ArchiveID) as [from],
md.ArchiveID - 1 as [to]
from tblArchives md
where md.ArchiveID != 1 and not exists (
select 1 from tblArchives md2 where md2.ArchiveID = md.ArchiveID - 1)
) SELECT [from], [to], ([to]-[from])+1 [total missing]
From CTERange
ORDER BY ([to]-[from])+1 DESC;
from to total missing
------- ------- --------------
6 6 1
8 8 1
11 14 4
DECLARE #TempSujith TABLE
(MissingId int)
Declare #Id Int
DECLARE #mycur CURSOR
SET #mycur = CURSOR FOR Select Id From tbl_Table
OPEN #mycur
FETCH NEXT FROM #mycur INTO #Id
Declare #index int
Set #index = 1
WHILE ##FETCH_STATUS = 0
BEGIN
if (#index < #Id)
begin
while #index < #Id
begin
insert into #TempSujith values (#index)
set #index = #index + 1
end
end
set #index = #index + 1
FETCH NEXT FROM #mycur INTO #Id
END
Select Id from tbl_Table
select MissingId from #TempSujith
Create a useful Tally table:
-- can go up to 4 million or 2^22
select top 100000 identity(int, 1, 1) Id
into Tally
from master..spt_values
cross join master..spt_values
Index it, or make that single column as PK.
Then use EXCEPT to get your missing number.
select Id from Tally where Id <= (select max(Id) from TestTable)
except
select Id from TestTable
You could also solve using something like a CTE to generate the full sequence:
create table #tmp(sequence int)
insert into #tmp(sequence) values (1)
insert into #tmp(sequence) values (2)
insert into #tmp(sequence) values (3)
insert into #tmp(sequence) values (5)
insert into #tmp(sequence) values (6)
insert into #tmp(sequence) values (8)
insert into #tmp(sequence) values (10)
insert into #tmp(sequence) values (11)
insert into #tmp(sequence) values (14)
DECLARE #max INT
SELECT #max = max(sequence) from #tmp;
with full_sequence
(
Sequence
)
as
(
SELECT 1 Sequence
UNION ALL
SELECT Sequence + 1
FROM full_sequence
WHERE Sequence < #max
)
SELECT
full_sequence.sequence
FROM
full_sequence
LEFT JOIN
#tmp
ON
full_sequence.sequence = #tmp.sequence
WHERE
#tmp.sequence IS NULL
Hmmmm - the formatting is not working on here for some reason? Can anyone see the problem?
i had made a proc so you can send the table name and the key and the result is a list of missing numbers from the given table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[action_FindMissing_Autoincremnt]
(
#tblname as nvarchar(50),
#tblKey as nvarchar(50)
)
AS
BEGIN
SET NOCOUNT ON;
declare #qry nvarchar(4000)
set #qry = 'declare #min int '
set #qry = #qry + 'declare #max int '
set #qry = #qry +'select #min = min(' + #tblKey + ')'
set #qry = #qry + ', #max = max('+ #tblKey +') '
set #qry = #qry + ' from '+ #tblname
set #qry = #qry + ' create table #tmp (Field_No int)
while #min <= #max
begin
if not exists (select * from '+ #tblname +' where '+ #tblKey +' = #min)
insert into #tmp (Field_No) values (#min)
set #min = #min + 1
end
select * from #tmp order by Field_No
drop table #tmp '
exec sp_executesql #qry
END
GO
SELECT TOP 1 (Id + 1)
FROM CustomerNumberGenerator
WHERE (Id + 1) NOT IN ( SELECT Id FROM CustomerNumberGenerator )
Working on a customer number generator for my company. Not the most efficient but definitely most readable
The table has one Id column.
The table allows for Ids to be inserted at manually by a user off sequence.
The solution solves the case where the user decided to pick a high number
Related
I want to create series data in SQL
I want to create series data in SQL for my testing purpose. Ex. Product1 Product2 Product3 . . . Product1000 I want to create 1000 records like above manner in SQL. Can anybody suggest me the solution ? Thank you
You can do that via recursive way if you are working with SQL Server: with series as ( select 1 as id union all select id+1 from series where id < 1000 ) select concat('Product', id) from series s option (maxrecursion 1000);
Just another option using an ad-hoc tally table Example Select Top 1000 Product=concat('Product',Row_Number() Over (Order By (Select NULL))) From master..spt_values n1 Returns Product1 Product2 Product3 Product4 ... Product998 Product999 Product1000
Here you go: DECLARE #temp TABLE( Products VARCHAR(15) ); DECLARE #counter INT SET #counter = 1 WHILE (#counter <= 1000) BEGIN INSERT INTO #temp VALUES('Product'+CONVERT(VARCHAR(5),#counter)); SET #counter = #counter + 1 END SELECT * FROM #temp;
This should get you started. DECLARE #table table (Product varchar(200)) DECLARE #cnt INT; SET #cnt = 0 WHILE #cnt <=10 BEGIN SET #cnt = #cnt + 1 insert into #table SELECT 'Product' +CAST(#cnt as varchar(150)) END select * from #table
A set-based CTE can generate test data quickly. Even better would be a tally (a.k.a.) numbers table. WITH t10 AS (SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t(n)) ,t1k AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS num FROM t10 AS a CROSS JOIN t10 AS b) SELECT 'Product' + CAST(num AS varchar(10)) FROM t1k;
dynamic alias in sql server
I want query field with different alias in stored procedure select COUNT(EmpCode) as CountEmp+#para result shoud be CountEmp1 45 CountEmp2 54 CountEmp1 76 Query loop in c# code: select COUNT(EmpCode) where something = #something as CountEmp+#para
Approach without dynamic SQL: --I create temp table for demonstration DECLARE #some_table TABLE ( Something int, EmpCode INT ) INSERT INTO #some_table (Something, EmpCode) VALUES (1, 10),(1, 22),(1, 12),(2, 12),(2, 30),(3, 65),(3, 15),(3, 11),(3, 5) --Declare parameter we want to search DECLARE #param int = 1 --Query --In cte we select what we need based on parameter ;WITH cte AS ( SELECT 'CountEmp'+CAST(#param as nvarchar(10)) as SomeThing, CAST(COUNT(EmpCode) as nvarchar(10)) as EmpCodeCount, ROW_NUMBER() OVER (ORDER BY SomeThing ) as rn FROM #some_table WHERE SomeThing = #param GROUP BY SomeThing ) --And here comes UNION SELECT SomeThing as Result FROM ( SELECT SomeThing,rn FROM cte UNION ALL SELECT EmpCodeCount ,rn FROM cte ) as t ORDER BY rn, SomeThing DESC Output: Result ------------------ CountEmp1 3 (2 row(s) affected)
Please try to make use of below code. Its working fine with SQL Server 2012. IF OBJECT_ID ('temp..#Mytable') IS NOT NULL CREATE TABLE #Mytable (ID INT IDENTITY (1,1),EmpCode INT) DECLARE #max int ,#count int SET #max =0; DECLARE #str varchar(10) INSERT #Mytable (EmpCode) VALUES (10), (45), (35), (63), (56), (65) SET #count = (SELECT COUNT (ID) FROM #Mytable ) WHILE #count > #max BEGIN SET #max = #max+1 SET #str = CONVERT(varchar(10),#max) EXEC('SELECT EmpCode AS Empcode'+#str+ ' FROM #Mytable WHERE ID = '+#str) END
SQL multiplying rows in select
I would like to select some rows multiple-times, depending on the column's value. Source table Article | Count =============== A | 1 B | 4 C | 2 Wanted result Article =============== A B B B B C C Any hints or samples, please?
You could use: SELECT m.Article FROM mytable m CROSS APPLY (VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) AS s(n) WHERE s.n <= m.[Count]; LiveDemo Note: CROSS APLLY with any tally table. Here values up to 10. Related: What is the best way to create and populate a numbers table?
You could also use a recursive CTE which works with numbers > 10 (here up to 1000): With NumberSequence( Number ) as ( Select 0 as Number union all Select Number + 1 from NumberSequence where Number BETWEEN 0 AND 1000 ) SELECT Article FROM ArticleCounts CROSS APPLY NumberSequence WHERE Number BETWEEN 1 AND [Count] ORDER BY Article Option (MaxRecursion 0) Demo A number-table will certainly be the best option. http://sqlperformance.com/2013/01/t-sql-queries/generate-a-set-2
Please check following SQL script Before executing the SELECT statement, note that I used a user function which is used to simulate a numbers table You can find the sql codes of numbers table in SQL Server at referred tutorial ----create table myTempTbl (Article varchar(10), Count int) --insert into myTempTbl select 'A',1 --insert into myTempTbl select 'B',4 --insert into myTempTbl select 'C',2 select t.* from myTempTbl t cross apply dbo.NumbersTable(1,100,1) n where n.i <= t.Count order by t.Article
one more CTE with cte_t as ( select c as c, 1 as i from mytable group by c union all select t.c, ctet.i + 1 from mytable t join cte_t ctet on ctet.c = t.c and ctet.i < t.i ) select cte_t.c from cte_t order by cte_t.c
Can obtain the output using simple WHILE LOOP DECLARE #table TABLE (ID int ,Article varchar(5),[Count] int) INSERT INTO #table (ID,Article,Count) VALUES (1,'A',1),(2,'B',4),(3,'C',2) DECLARE #temp TABLE (Article varchar(5)) DECLARE #Cnt1 INT DECLARE #Cnt2 INT DECLARE #Check INT DECLARE #max INT SET #max =0 SET #Cnt1 = (SELECT Count(Article) FROM #table) WHILE (#max < #Cnt1) BEGIN SET #max = #max +1 SET #Cnt2 = (SELECT [Count] FROM #table WHERE ID =#max) SET #Check =(SELECT [Count] FROM #table WHERE ID =#max) WHILE (#Cnt2 > 0) BEGIN INSERT INTO #temp SELECT Article FROM #table WHERE [Count] =#Check SET #Cnt2 = #Cnt2 -1 END END SELECT * FROM #temp
Convert Comma Delimited String to bigint in SQL Server
I have a varchar string of delimited numbers separated by commas that I want to use in my SQL script but I need to compare with a bigint field in the database. Need to know to convert it: DECLARE #RegionID varchar(200) = null SET #RegionID = '853,834,16,467,841,460,495,44,859,457,437,836,864,434,86,838,458,472,832,433,142,154,159,839,831,469,442,275,840,299,446,220,300,225,227,447,301,450,230,837,441,835,302,477,855,411,395,279,303' SELECT a.ClassAdID, -- 1 a.AdURL, -- 2 a.AdTitle, -- 3 a.ClassAdCatID, -- 4 b.ClassAdCat, -- 5 a.Img1, -- 6 a.AdText, -- 7 a.MemberID, -- 9 a.Viewed, -- 10 c.Domain, -- 11 a.CreateDate -- 12 FROM ClassAd a INNER JOIN ClassAdCat b ON b.ClassAdCAtID = a.ClassAdCAtID INNER JOIN Region c ON c.RegionID = a.RegionID AND a.PostType = 'CPN' AND DATEDIFF(d, GETDATE(), ExpirationDate) >= 0 AND a.RegionID IN (#RegionID) AND Viewable = 'Y' This fails with the following error: Error converting data type varchar to bigint. RegionID In the database is a bigint field.. need to convert the varchar to bigint.. any ideas..? Many thanks in advance, neojakey
create this function: CREATE function [dbo].[f_split] ( #param nvarchar(max), #delimiter char(1) ) returns #t table (val nvarchar(max), seq int) as begin set #param += #delimiter ;with a as ( select cast(1 as bigint) f, charindex(#delimiter, #param) t, 1 seq union all select t + 1, charindex(#delimiter, #param, t + 1), seq + 1 from a where charindex(#delimiter, #param, t + 1) > 0 ) insert #t select substring(#param, f, t - f), seq from a option (maxrecursion 0) return end change this part: AND a.RegionID IN (select val from dbo.f_split(#regionID, ',')) Change this for better overall performance: AND DATEDIFF(d, 0, GETDATE()) <= ExpirationDate
Your query does not know that those are separate values, you can use dynamic sql for this: DECLARE #RegionID varchar(200) = null SET #RegionID = '853,834,16,467,841,460,495,44,859,457,437,836,864,434,86,838,458,472,832,433,142,154,159,839,831,469,442,275,840,299,446,220,300,225,227,447,301,450,230,837,441,835,302,477,855,411,395,279,303' declare #sql nvarchar(Max) set #sql = 'SELECT a.ClassAdID, -- 1 a.AdURL, -- 2 a.AdTitle, -- 3 a.ClassAdCatID, -- 4 b.ClassAdCat, -- 5 a.Img1, -- 6 a.AdText, -- 7 a.MemberID, -- 9 a.Viewed, -- 10 c.Domain, -- 11 a.CreateDate -- 12 FROM ClassAd a INNER JOIN ClassAdCat b ON b.ClassAdCAtID = a.ClassAdCAtID INNER JOIN Region c ON c.RegionID = a.RegionID AND a.PostType = ''CPN'' AND DATEDIFF(d, GETDATE(), ExpirationDate) >= 0 AND a.RegionID IN ('+#RegionID+') AND Viewable = ''Y''' exec sp_executesql #sql
I use this apporach sometimes and find it very good. It transfors your comma-separated string into an AUX table (called #ARRAY) and then query the main table based on the AUX table: declare #RegionID varchar(50) SET #RegionID = '853,834,16,467,841,460,495,44,859,457,437,836,864,434,86,838,458,472,832,433,142,154,159,839,831,469,442,275,840,299,446,220,300,225,227,447,301,450,230,837,441,835,302,477,855,411,395,279,303' declare #S varchar(20) if LEN(#RegionID) > 0 SET #RegionID = #RegionID + ',' CREATE TABLE #ARRAY(region_ID VARCHAR(20)) WHILE LEN(#RegionID) > 0 BEGIN SELECT #S = LTRIM(SUBSTRING(#RegionID, 1, CHARINDEX(',', #RegionID) - 1)) INSERT INTO #ARRAY (region_ID) VALUES (#S) SELECT #RegionID = SUBSTRING(#RegionID, CHARINDEX(',', #RegionID) + 1, LEN(#RegionID)) END select * from your_table where regionID IN (select region_ID from #ARRAY) It avoids you from ahving to concatenate the query string and then use EXEC to execute it, which I dont think it is a very good approach. if you need to run the code twice you will need to drop the temp table
I think the answer should be kept simple. Try using CHARINDEX like this: DECLARE #RegionID VARCHAR(200) = NULL SET #RegionID = '853,834,16,467,841,460,495,44,859,457,437,836,864,434,86,838,458,472,832,433,142,154,159,839,831,469,442,275,840,299,446,220,300,225,227,447,301,450,230,837,441,835,302,477,855,411,395,279,303' SELECT 1 WHERE Charindex('834', #RegionID) > 0 SELECT 1 WHERE Charindex('999', #RegionID) > 0 When CHARINDEX finds the value in the large string variable, it will return it's position, otherwise it return 0. Use this as a search tool.
The easiest way to change this query is to replace the IN function with a string function. Here is what I consider the safest approach using LIKE (which is portable among databases): AND ','+#RegionID+',' like '%,'+cast(a.RegionID as varchar(255))+',%' Or CHARINDEX: AND charindex(','+cast(a.RegionID as varchar(255))+',', ','+#RegionID+',') > 0 However, if you are explicitly putting the list in your code, why not use a temporary table? declare #RegionIds table (RegionId int); insert into #RegionIds select 853 union all select 834 union all . . . select 303 Then you can use the table in the IN clause: AND a.RegionId in (select RegionId from #RegionIds) or in a JOIN clause.
I like Diego's answer some, but I think my modification is a little better because you are declaring a table variable and not creating an actual table. I know the "in" statement can be a little slow, so I did an inner join since I needed some info from the Company table anyway. declare #companyIdList varchar(1000) set #companyIdList = '1,2,3' if LEN(#companyIdList) > 0 SET #companyIdList = #companyIdList + ',' declare #CompanyIds TABLE (CompanyId bigint) declare #S varchar(20) WHILE LEN(#companyIdList) > 0 BEGIN SELECT #S = LTRIM(SUBSTRING(#companyIdList, 1, CHARINDEX(',', #companyIdList) - 1)) INSERT INTO #CompanyIds (CompanyId) VALUES (#S) SELECT #companyIdList = SUBSTRING(#companyIdList, CHARINDEX(',', #companyIdList) + 1, LEN(#companyIdList)) END select d.Id, d.Name, c.Id, c.Name from [Division] d inner join [Company] c on d.CompanyId = c.Id inner join #CompanyIds cids on c.Id = cids.CompanyId
How to find missing id in the table
I have column looks like below SID101 SID102 SID103 SID105 SID107 In the above criteria i need to find missed SID numbers. SID104 and SID 106 are missed while ordering. How can i find the missed id numbers.Could any one help me finding it. Thanks in advance.
If your table contains gaps with length more than 1 item, you can use this query: declare #t table(s varchar(20)) insert #t values ('SID101'),('SID102'),('SID103'),('SID105'),('SID108'); with cte as ( select substring(t.s, 4, len(t.s)) [i] from #t t ) select 'SID' + cast(m.number as varchar(20)) from master..spt_values m left join cte c on c.i = m.number where [Type] = 'P' and m.number >= (select min(i) from cte) and m.number <= (select max(i) from cte) and c.i is null Output: ----------------------- SID104 SID106 SID107
Something like this should work: DECLARE #i INT; SET #i = 100; CREATE TABLE #idsToCheck (checkId varchar(100)); WHILE (#i < 200) BEGIN INSERT INTO #idsToCheck VALUES ('SID' + CONVERT(varchar(100), #i)); SET #i = #i + 1; END SELECT * FROM #idsToCheck itc LEFT OUTER JOIN MainTable mt ON itc.checkId = mt.realId WHERE mt.realId = NULL DROP TABLE #idsToCheck ... where MainTable is your table containing the SID101, SID102, etc. column values, and MainTable.realId is the column containing those IDs. Modify the #i initial value and number in the while loop condition based on which SIDs you want to check from/to.
It's difficult. With SELECT COUNT(*),MAX(CAST(REPLACE(y.name,'SID','') AS INT)) AS col_max FROM sys.objects x INNER JOIN sys.columns y ON x.object_id=y.object_id WHERE x.name='<TABLE_NAME>' you should know, how much columns are missing (i.e. COUNT(*) is 5 and col_max is 107) When you have a table, which contains only one column with all possible IDs from 1 to max (i.e. 100,101,102,103,104,...,132) then you could do SELECT * FROM ( SELECT CAST(REPLACE(y.name,'SID','') AS INT) AS col_id FROM sys.objects x INNER JOIN sys.columns y ON x.object_id=y.object_id WHERE x.name='<TABLE_NAME>' ) a RIGHT JOIN <TABLE_IDS> b ON a.col_id=b.id WHERE a.col_id IS NULL AND b.id<=( SELECT MAX(CAST(REPLACE(y.name,'SID','') AS INT)) AS col_max FROM sys.objects x INNER JOIN sys.columns y ON x.object_id=y.object_id WHERE x.name='<TABLE_NAME>' ) EDIT: sorry, I've seen just now, that these values aren't column names, but values. My solution will find missing column names
Declare #St int declare #end int set #st = CAST( (select RIGHT( max(data),4) from orderno)as int) set #end = CAST( (select RIGHT( min(data),4) from orderno)as int) create table #temp(data int) while(#St <= #end ) begin insert into #temp values(#St) set #St = #St +1 end select * from orderno select * from #temp select data from #temp where data not in (select cast(RIGHT(data,4))
declare #t table(s varchar(20)) insert #t values ('SID101'),('SID102'),('SID103'),('SID105'),('SID107'); with cte as ( select substring(t.s, 4, len(t.s)) [i] from #t t ) select 'SID' + cast(t1.i + 1 as varchar(20)) from cte t1 join cte t2 on t2.i > t1.i and not exists( select 1 from cte c3 where c3.i > t1.i and c3.i < t2.i ) where t2.i <> t1.i + 1 Output: ----------------------- SID104 SID106