Query query!

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Query query!

 
0
  #1
Oct 8th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 41
Reputation: huangzhi is an unknown quantity at this point 
Solved Threads: 13
huangzhi huangzhi is offline Offline
Light Poster

Re: Query query!

 
0
  #2
Oct 11th, 2008
Try code below:
  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
Hence Wijaya
www.ex-Soft.tk
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC