Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 June Script to Validate IP Address range in SQL Server

Script to Validate IP Address range in SQL Server

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demonstration on how to validate the range of IP Address in SQL Server.

You can set CHECK CONSTRAINT for each part of IP Address for validating a proper range.

For example,
You require inserting IP Address between 10.20 range. This you can achieve using PARSENAME function of SQL Server.

Below is a full demo on this:

Let’s first create one table and define CHECK Constraint using PARSENAME.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE TABLE tbl_IPAddress
(
IPAddress VARCHAR(15)
)
GO
 
ALTER TABLE tbl_IPAddress ADD CONSTRAINT chk_tbl_IPAddress_IPAddress
CHECK
(
(ParseName(IPAddress, 4) = 12)
AND (ParseName(IPAddress, 3) = 4)
AND (ParseName(IPAddress, 2) BETWEEN 8 AND 26)
AND (ParseName(IPAddress, 1) BETWEEN 1 AND 255)
)
GO

You can see different four part validation ,

First two part validation is: only IP Address range start with 12.4

Last two part validation is: 8 to 26 and 1 to 255.

Means valid IP Address are :

12.4.8.125

12.4.15.241

Invalid IP Address are:

10.2.21.52

12.4.7.124

Now insert script for some valid IP Address:

1
2
3
4
5
6
7
8
INSERT INTO tbl_IPAddress (IPAddress)
VALUES
('12.4.8.34')
,('12.4.16.125')
,('12.4.22.120')
,('12.4.22.120')
,('12.4.15.216')
GO

If you tried to insert out of range, then this will show you an error that you cannot violate check constraint.

Script to Select different part of stored IP Address:

1
2
3
4
5
6
SELECT IPAddress
,ParseName(IPAddress, 4) As FirstPart
,ParseName(IPAddress, 3) As SecondPart
,ParseName(IPAddress, 2) As ThirdPart
,ParseName(IPAddress, 1) As FourthPart
FROM tbl_IPAddress

My Suggestion:

In this demo, I stored IP Address into one VARCHAR(15) column which is not effective storage.
Whenever you don’t require any analysis on IP Address, then you can store the IP Address into one column. Otherwise, please visit this page on the effective storage of IP Addresses.

Jun 1, 2015Anvesh Patel
How to find size of Database and Table in PostgreSQLCopy unique records from one table to another in SQL Server
Comments: 1
  1. Raquel
    November 19, 2015 at 5:23 pm

    Thanks Anvesh! you helped me lot…..I added your blog into my bookmark.

Anvesh Patel
Anvesh Patel

Database Engineer

June 1, 2015 SQL ServerAnvesh Patel, Check Constraint, database, database research and development, ip address, ParseName, SQL Query, SQL Server, SQL Server Administrator, SQL Server Tips and Tricks, TSQL, validation
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....