0

Please Help me to find the SQL Query I want to restrict user to enter duplicate value between From and To.

I have written the Query but it's not working, Here is my Query

SELECT intpkPciid 
FROM Table1  
WHERE @intFrom(userinputdata)  BETWEEN intFrom AND intTO

enter image description here

strickt01
  • 3,959
  • 1
  • 17
  • 32
Imran Azam
  • 89
  • 9
  • Query? SQL? Or do you just want to know how to format a string? – Tim Rutter Apr 04 '16 at 14:13
  • Actually my requirement is:- These numbers From and To are related with some Package id. So i want to update all data in another table column like 9-10,5-5-6,90-100,100-200. SQL Query i want. Value From and To i can Concate but in comma Seperated i want pls help me out. – Imran Azam Apr 05 '16 at 05:17
  • If its sql then add that tag. – Tim Rutter Apr 05 '16 at 05:43
  • What happened to your original question re. comma-separated values? The nature of your question seems to have completely changed. – strickt01 Apr 05 '16 at 07:30

1 Answers1

0

You can merge all your rows into comma-separated values using COALESCE:

DECLARE @test varchar(max)

SELECT @test = COALESCE(@test + ',', '') + CAST(intFrom as varchar) + '-' + CAST(intTo as varchar)
from Table1

SELECT @test

Your question seems to have completely changed, but you can avoid row entries that overlap using:

SELECT intpkPciid FROM Table1 WHERE @intFrom <= intTO AND @intTo >= intFrom
strickt01
  • 3,959
  • 1
  • 17
  • 32
  • Are you aware that `SELECT @test = @test + ... ` is unreliable? [More info](http://stackoverflow.com/a/15163136/5070879) – Lukasz Szozda Apr 05 '16 at 08:00
  • Point taken @lad2025. However, it does usually work, especially where the query plan is as simple as this one. i.e. a single clustered index scan. – strickt01 Apr 05 '16 at 08:14
  • i don't have 15 reputation so i am not able to mark it as answered. – Imran Azam Apr 05 '16 at 12:40
  • From my understanding, you can't vote up the answer until you have 15 reputation but you can mark the answer as accepted. – strickt01 Apr 05 '16 at 12:48
  • @ImranAzam - you can mark an answer as "accepted" with less than 15 reputation - https://stackoverflow.com/help/privileges/create-posts. You can't vote below that level. – ChrisF Apr 07 '16 at 10:32