473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to can I pull the last two octets from an IP address in SQL?

Seth Schrock
2,965 Expert 2GB
I'm trying to the last two portions of a devices IP address (which is in a field) and use it as part of a device ID. Right now I'm using the MID function in SQL to get the 9th through the last digit. This works fine for our IP addresses which are like 192.168.0.1 (most of them), but it doesn't work for the IP addresses that are like 12.232.100.100 (a few of them). I would like to be able to have it look for everything after the second period, but I haven't been able to find anything online for that. Is this possible?

Here is the code I'm using now:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblDevice.Company, 
  2. tblDevice.Branch, 
  3. tblDevice.ComputerName, 
  4. tblDevice.Department, 
  5. tblDevice.Type, 
  6. tblDevice.Manufacturer, 
  7. tblDevice.Model, 
  8. tblDevice.SerialNumber, 
  9. tblDevice.ExtendedServiceCode, 
  10. tblDevice.PurchaseDate, 
  11. tblDevice.Warranty, 
  12. tblDevice.Vender, 
  13. tblDevice.OperatingSystem, 
  14. tblDevice.OfficeVersion, 
  15. tblDevice.Status, 
  16. tblDevice.UsageHistory, 
  17. tblDevice.Processor, 
  18. tblDevice.RAM, 
  19. tblDevice.HDD, 
  20. tblDevice.HDDType, 
  21. tblDevice.HDDRemoved, 
  22. (SELECT TOP 1 IPAddress FROM tblIPAddress WHERE DeviceID = SerialNumber) AS IPAddress, 
  23. Year(PurchaseDate) & Month(PurchaseDate) & ('-'+SerialNumber) & ('-'+Mid(IPAddress,9)) AS DeviceID
  24. FROM tblDevice;
  25.  
Jun 29 '12 #1

✓ answered by Rabbit

Use the InStr function to find the location of the first dot. Use that plus 1 as the start parameter for another InStr function to find the second dot. Now that you know where the second dot is, you can use the Mid function to grab everything after that point.

2 4486
Rabbit
12,516 Expert Mod 8TB
Use the InStr function to find the location of the first dot. Use that plus 1 as the start parameter for another InStr function to find the second dot. Now that you know where the second dot is, you can use the Mid function to grab everything after that point.
Jun 29 '12 #2
Seth Schrock
2,965 Expert 2GB
Thank-you Rabbit. That was exactly what I needed. For anyone else viewing this, I replaced line 23 from my OP to be:
Expand|Select|Wrap|Line Numbers
  1. Year(PurchaseDate) & Month(PurchaseDate) & ('-'+SerialNumber) & ('-'+Mid(IPAddress,InStr(InStr(IPAddress,".")+1,IPAddress,".")+1)) AS DeviceID
Jun 29 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Richard T. Cunningham | last post by:
I've been using two instances of substr to pull an IP address out of a string. I know there's a more efficient way. Anyone? The string is like this: HOST 61.2.135.45 blocked. Richard
5
by: cvillard | last post by:
I am new to SQL, as old as it is. I am not new to programming I normally just use Access. I have two tables for a little project manager I made. After updates I sent an email to the user. I need...
65
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. ...
10
by: bobBaker | last post by:
Does anybody know of a good way to get a list of emails from the corporate exchange server from a corporate website?
5
by: RThaden | last post by:
Hi all, I have a text file with a list of MAC addresses. Each time, my program is called it reads the last MAC address entry from the file, increases it by one, writes this new address into a 6...
5
by: amanda27 | last post by:
I have a database that we use in our department for the status of our projects. In the form when you pick a project from the dropdown list I have a subform that pulls the data entered for the...
14
by: NetworkElf | last post by:
Hi all, Does anyone have some code that shows an example of how to loop through a range of IP addresses? I'm using text boxes to get a start and end value for the range. I was thinking about...
0
by: Harlin Seritt | last post by:
Is there a module that can pull str values for say the last 3 months? Something like: print lastMonths(3) Thanks
1
by: barmatt80 | last post by:
there is a website i was asked to look at ( i have no clue why as i have never done much web development expecially with frontpage extensions). they want to setup a form on the page that will...
1
by: dewed | last post by:
hello, I have a simple form. some text boxes for customername , date and a code for each customer...i want my secretary not to get access to other columns which are about prices..just to enter the 3...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.