Ver Mensaje Individual
  #2 (permalink)  
Antiguo 28/07/2014, 16:26
Avatar de Libras
Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 9 meses
Puntos: 774
Respuesta: Comparando rangos de numeros

Código SQL:
Ver original
  1. CREATE TABLE #temp
  2. (
  3. id INT,
  4. tipo VARCHAR(20),
  5. inicio INT,
  6. fin INT
  7. )
  8.  
  9.  
  10. INSERT INTO #temp VALUES (1,'tipo 1',300,600)
  11. INSERT INTO #temp VALUES (2,'tipo 1',800,900)
  12. INSERT INTO #temp VALUES (3,'tipo 1',950,970)
  13.  
  14.  
  15. DECLARE @inicio INT
  16. DECLARE @fin INT
  17. SET @inicio=240
  18. SET @fin=250
  19.  
  20. IF (
  21. SELECT COUNT(test) + COUNT(test2) FROM (
  22. SELECT CASE WHEN @inicio BETWEEN inicio AND fin THEN 'si' ELSE 'no' END AS test,
  23. CASE WHEN @fin BETWEEN inicio AND fin THEN 'si' ELSE 'no' END AS test2
  24. FROM #temp ) AS t1 WHERE test='si' OR test2='si')>=1
  25. print 'Rango no disponible'
  26. ELSE
  27.     INSERT INTO #temp VALUES (1,'tipo 1',@inicio,@fin)

Con algo como eso obtienes lo que necesitas :)
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me