vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
Erstellen von dynamischen Kontextmen?s - wann immer Sie sie brauchen!  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2025
 
zurück

 Sie sind aktuell nicht angemeldet.Funktionen: Einloggen  |  Neu registrieren  |  Suchen

VB.NET - Ein- und Umsteiger
Re: Umwandlung von IP Adressen im Grid wegen korrekter Sortierung 
Autor: rkra
Datum: 30.08.09 21:20

Hallo,
zu einer eigener Sortier-Methode habe ich es nicht geschafft. Eine längere Suche zeigte mir jedoch einen superleichten Weg. Wenn man einen SQL Server hat so sollte er es auch sortieren können. Dazu einfach da ORDER BY statement erweitern und fertig ist es. Hier ist der englische Text und der Link :

http://www.sql-server-helper.com/tips/sort-ip-address.aspx

IPAddress
---------------
10.0.0.1
192.120.40.243
207.46.199.60
255.255.0.0
255.255.255.255
64.233.188.15
65.54.152.142
65.97.176.172
68.142.197.0
98.123.251.21

As can be seen from this output, the IP addresses are sorted by its VARCHAR value instead of its numeric value. The IP address '255.255.255.255' should be the last one listed but instead, it is in the fifth because '2', the first character in the IP address, comes before '6', which is the first character of the next IP address.

PARSENAME System Function To The Rescue

Sorting IP addresses by its numeric value is made easy in SQL Server thanks to the PARSENAME system function. Technically, the main purpose of the PARSENAME system function is to return the specified part of an object name, with the parts of an object being the object name, owner name, database name and server name. When we say object here, we mean either a table, view or any SQL Server object.

Since the naming convention of a SQL Server object is quite similar to the naming convention of an IP address, similar in the sense that they both use a period (.) to separate each part and both have a maximum of 3 periods, we can use the PARSENAME system function to get the different parts of an IP address.

One good thing about the PARSENAME system function is that it does not indicate whether or not an object by the specified name exists. It just returns the specified part of the given input.

To put the PARSENAME system function into use, the following SELECT statement will return the IP addresses sorted by its numeric value instead of by the VARCHAR value:

SELECT [IPAddress] FROM [WorkStation]
ORDER BY CAST(PARSENAME([IPAddress], 4) AS INT),
CAST(PARSENAME([IPAddress], 3) AS INT),
CAST(PARSENAME([IPAddress], 2) AS INT),
CAST(PARSENAME([IPAddress], 1) AS INT)


The output of this SELECT statement is the following, which sorts the IP addresses as required.

IPAddress
---------------
10.0.0.1
64.233.188.15
65.54.152.142
65.97.176.172
68.142.197.0
98.123.251.21
192.120.40.243
207.46.199.60
255.255.0.0
255.255.255.255
alle Nachrichten anzeigenGesamtübersicht  |  Zum Thema  |  Suchen

 ThemaViews  AutorDatum
Umwandlung von IP Adressen im Grid wegen korrekter Sortierun...1.243rkra14.08.09 20:27
Re: Umwandlung von IP Adressen im Grid wegen korrekter Sorti...621Manni0114.08.09 21:46
Re: Umwandlung von IP Adressen im Grid wegen korrekter Sorti...640rkra15.08.09 10:07
Re: Umwandlung von IP Adressen im Grid wegen korrekter Sorti...639rkra30.08.09 21:20

Sie sind nicht angemeldet!
Um auf diesen Beitrag zu antworten oder neue Beiträge schreiben zu können, müssen Sie sich zunächst anmelden.

Einloggen  |  Neu registrieren

Funktionen:  Zum Thema  |  GesamtübersichtSuchen 

nach obenzurück
 
   

Copyright ©2000-2025 vb@rchiv Dieter Otter
Alle Rechte vorbehalten.
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein.

Diese Seiten wurden optimiert für eine Bildschirmauflösung von mind. 1280x1024 Pixel