943,536 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 724
  • MS SQL RSS
Oct 8th, 2008
0

Query query!

Expand Post »
Hi everyone,

I'm trying to write a stored procedure select statement which is a little beyond my experience level.

I have a table in the DB called addresses which contains the following fields:
  • address, VARCHAR
  • count, INT
  • (a few more fields)

My stored procedure will take in a unique address in the following format:
  • ####.####.####.####, VARCHAR

What I need to do is compare the passed in value against the [addresses].[address] values and return the matching row. The problem is I also need to check for for increments on the final four characters of the passed in address, upto the [addresses].[count].

Here is an example, the address table contains:
  • address = 100.100.2225.100
  • count = 5
  • (a few more fields)

The stored procedure value passed in is 100.100.2225.103. In this case thie row should be returned as the passed in address 100.100.2225.103 <= 100.100.2225.(100+5)

I hope this makes sense to someone and is simple to do. This isn't for any specific project or work and is purely for my own personal geekery. My only other option is to do this in code.

Thanks in advance for any pointers, code, links or abuse.
Similar Threads
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Oct 11th, 2008
0

Re: Query query!

Try code below:
MS SQL Syntax (Toggle Plain Text)
  1. declare @Ask varchar(255)
  2.  
  3. SELECT @Ask = '100.100.2225.104'
  4.  
  5. CREATE TABLE #tmpAddr (
  6. Addr varchar(255),
  7. Inc int)
  8.  
  9. INSERT #tmpAddr select '100.100.2225.100', 5
  10. INSERT #tmpAddr select '100.100.2225.103', 5
  11. INSERT #tmpAddr select '100.100.2225.110', 5
  12.  
  13. SELECT LEFT(Addr, len(Addr) - charindex('.', reverse(Addr))),
  14. RIGHT(Addr, charindex('.', reverse(Addr)) - 1)
  15. FROM #tmpAddr
  16. WHERE @Ask BETWEEN Addr AND LEFT(Addr, len(Addr) - charindex('.', reverse(Addr))) + '.' + cast(cast(RIGHT(Addr, charindex('.', reverse(Addr)) - 1) AS int) + Inc AS varchar)
  17.  
  18. DROP TABLE #tmpAddr
Reputation Points: 10
Solved Threads: 13
Light Poster
huangzhi is offline Offline
48 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: MSSQL charindex HELP !
Next Thread in MS SQL Forum Timeline: Codings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC