Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Afternoon to all Developers,

The following is my "ODRange" table structure, Now if user enters the range

Minimum=23 Maximum=30 --Then its valid because that range doesn't exists in the table
Minimum=35 Maximum=40 --Invalid, Because the range b/n 31 to 37 already exists.Overlapping Should be avoided

So how to check whether the user enters the correct range or not, On basis of that comparision, We have to display whether its valid or not..

Minimum  Maximum
1	10
11	22
31	37
45	52
58	69
70	86
87	90
97	102
103	107


The range should be inserted at the particular position.

Thanks in Advance for any suggestion..!
Posted
Updated 24-Apr-12 0:49am
v2

use 2 parameters in sqlcommand object for min and max entered by user
create datareader to run query select * from tab where min=@1 and max=@2
and see if dr.HasRows return true for simple (dr is object of DataReader)

oR


use exists in SQl query
the link for using details is

http://msdn.microsoft.com/en-us/library/ms188336.aspx[^]


use any option you prefer

Best of Luck!!
 
Share this answer
 
Comments
The Doer 24-Apr-12 7:59am    
sorry,am not using .net, its SQL server.

We can't use exists here because
Minimum=23 Maximum=30
is not exists still its invalid, and there's no function as overlap in SQL Server..
The overlap can be find using the following query:
SQL
IF EXISTS (SELECT 1 FROM ODRange WHERE 31 <= maximum AND 37 >= Minimum)
  print 'Overlaps'
ELSE
  print 'Does not overlap'


--Amit
 
Share this answer
 
Comments
The Doer 27-Jun-13 23:05pm    
sahi he kamine , Jeete raho! +5
_Amy 27-Jun-13 23:09pm    
Welcome. ;)
Hi,
Following will solve your purpose..

SQL
declare @min int
declare @max int

set @min= 35
set @max= 40


if exists(select * from ODRange where (minimum<=@min and maximum >=@min) or (minimum<=@max and maximum >=@max))
print 'Invalid Range'
else
insert into ODRange values(@min,@max)
 
Share this answer
 
Hi,

Check the below sample...
SQL
--Step 1

--CReate the sample table


CREATE TABLE [OD] (
	[Mini] [int] NULL ,
	[maxi] [int] NULL 
) ON [PRIMARY]
GO


--Step 2 

--Insert some sample values

INSERT INTO OD (Mini, Maxi) VALUES (7,20)
INSERT INTO OD (Mini, Maxi) VALUES (40,50)
INSERT INTO OD (Mini, Maxi) VALUES (80,100)


--Step 3

--Check the validity by using the below query

DECLARE @MinVal AS INT
DECLARE @MaxVal AS INT

--input parameters
SET @MinVal = 87
SET @MaxVal = 90
--business rules to chk whether the new values overlap with existing entry
--it will display some records if the values overlap 
--If no records given as output- then the values are VALID

IF EXISTS (SELECT  OD.Mini, OD.Maxi 
FROM OD
WHERE (@MinVal >= OD.Mini AND @MinVal <=OD.Maxi)
OR (@MaxVal >= OD.Mini AND @MaxVal <=OD.Maxi)
OR (OD.Mini >= @MinVal AND OD.Mini <=@MaxVal) )
SELECT 'Invalid' 'Status'
ELSE
SELECT 'Valid' 'Status'

-- to display all the records
Select OD.Mini, OD.Maxi from OD

Regards,
GVPrabu
 
Share this answer
 
Comments
The Doer 27-Jun-13 23:12pm    
thanks prabu, but 1st where condition is enough i guess as _Amy specified.
Anyhow the Answer was correct so accepted.
gvprabu 28-Jun-13 5:27am    
I think some scenario... we need all this condition... so check all possible combinations

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900