Solved

storing IP addresses in SQL database and sorting

Posted on 2014-03-19
6
584 Views
Last Modified: 2014-03-19
developing an app in lightswitch using data stored in SQL server.    one of the fields is IP address and I need to be able to sort by IP address.  

One suggestion was to split the ip into four columns 3 digits each but that is ugly for entry and reporting.

the other was to enter the IP address with zeros such as 192.168.001.001 again not a good option

the third options was try to create a computed column that coverts the IP address to a sortable number by inserting the zeros or converting binary whatever is necessary

I think this is the best option but I have to idea how to do this in SQL I created a computed column but need assistance with the formula
0
Comment
Question by:Zoldy2000
  • 3
  • 3
6 Comments
 
LVL 12

Accepted Solution

by:
AshleyBryant earned 500 total points
ID: 39939567
Your best bet for padding the IP with 0s is to do it at the time of storage rather than using a computed column.  You could either do this in your code at the time that it's saved, make a trigger that does it for you, or just call a scalar function to get the value.  That being said, to answer your original question of how to do this with a computed column, you would make the column invoke a scalar function like the one below:

CREATE FUNCTION dbo.PadIP 
(
	@IP_IN varchar(15)
)
RETURNS varchar(15)
AS
BEGIN
	DECLARE @IP_OUT varchar(15) = ''
	DECLARE @chunk varchar(3) = ''

	WHILE (CHARINDEX('.',@IP_IN) > 0)
	BEGIN
		SELECT @chunk = LTRIM(RTRIM(SUBSTRING(@IP_IN, 1, CHARINDEX('.', @IP_IN) - 1)))
		SELECT @IP_OUT = @IP_OUT + REPLICATE('0', 3 - LEN(@chunk)) + @chunk + '.'
		SET @IP_IN = SUBSTRING(@IP_IN, CHARINDEX('.', @IP_IN) + 1, LEN(@IP_IN))
	END

	SELECT @IP_OUT = @IP_OUT + REPLICATE('0', 3 - LEN(@IP_IN)) + @IP_IN

	RETURN @IP_OUT
END

Open in new window


Run that script on your server.  To test it, try running a query like this:

SELECT dbo.PadIP ('8.12.134.10')

Open in new window


You should get the following output:  008.012.134.010

To make your computed column use the function, if memory serves, you would enter your Formula as dbo.padIP([Name_of_Unpadded_IP_Field]).
0
 
LVL 2

Author Comment

by:Zoldy2000
ID: 39939611
I don't really like modify the actual data.   It makes it messy when reporting and viewing.   I will try your suggestion on the computed column and see if that works.
0
 
LVL 12

Expert Comment

by:AshleyBryant
ID: 39939666
To be a little more clear, I meant that you should store the padded IP address in another column, not replace the existing one.  That way you could produce the padded IP address once and store it to use for sorting.  The padded IP column would never have to be used for display.
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 2

Author Comment

by:Zoldy2000
ID: 39939706
sorry I know this is a dumb question but how do i run that script on server?    Does it then get stored somewhere for use?
0
 
LVL 12

Expert Comment

by:AshleyBryant
ID: 39939733
Assuming you are using SQL Server Management Studio:

1. Right-click on your database name.
2. Click on "New Query".
3. Paste the script from above in the query window.
4. Click the "! Execute" button or hit the F5 key.

This will store the function in the database so that it can be used at any time.
0
 
LVL 2

Author Comment

by:Zoldy2000
ID: 39939780
Great work...   that worked perfectly thanks....
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Trying to get a Linked Server to Oracle DB working 21 67
Webservices in T-SQL 3 33
MS SQL + Insert Into Table - If Doesnt Exist 9 47
Job - date manual 1 35
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question