dbTalk Databases Forums  

Converting integer -> IP Address

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss Converting integer -> IP Address in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
brunoalsantos
 
Posts: n/a

Default Converting integer -> IP Address - 09-27-2010 , 12:30 PM






Hello all.

DB2 LUW V9.5 Fp5

Anyone knows a way to transform a integer in a ip address using Scalar
Function?

Regards

Bruno.

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Converting integer -> IP Address - 09-27-2010 , 01:29 PM






On 2010-09-27 19:30, brunoalsantos wrote:
Quote:
Hello all.

DB2 LUW V9.5 Fp5

Anyone knows a way to transform a integer in a ip address using Scalar
Function?

Is it something like this that you mean?

create function bigint_to_ip (ip bigint)
returns varchar(15)
begin atomic

declare fst bigint;
declare snd bigint;
declare trd bigint;
declare fth bigint;

set fst = ip / (256*256*256);
set snd = mod(ip,256*256*256) / (256*256);
set trd = mod(ip,256*256) / 256;
set fth = mod(ip,256);

return rtrim(cast(fst as char(3))) || '.' ||
rtrim(cast(snd as char(3))) || '.' ||
rtrim(cast(trd as char(3))) || '.' ||
rtrim(cast(fth as char(3)));

end @

db2 "values bigint_to_ip(3555336315)"

1
---------------
211.234.32.123


/Lennart





Quote:
Regards

Bruno.

Reply With Quote
  #3  
Old   
Tonkuma
 
Posts: n/a

Default Re: Converting integer -> IP Address - 09-27-2010 , 01:56 PM



On Sep 28, 3:29*am, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com>
wrote:
Quote:
On 2010-09-27 19:30, brunoalsantos wrote:

Hello all.

DB2 LUW V9.5 Fp5

Anyone knows a way to transform a integer in a ip address using Scalar
Function?

Is it something like this that you mean?

create function bigint_to_ip (ip bigint)
returns varchar(15)
begin atomic

* * declare fst bigint;
* * declare snd bigint;
* * declare trd bigint;
* * declare fth bigint;

* * set fst = ip / (256*256*256);
* * set snd = mod(ip,256*256*256) / (256*256);
* * set trd = mod(ip,256*256) / 256;
* * set fth = mod(ip,256);

* * return rtrim(cast(fst as char(3))) || '.' ||
* * * * * *rtrim(cast(snd as char(3))) || '.' ||
* * * * * *rtrim(cast(trd as char(3))) || '.' ||
* * * * * *rtrim(cast(fth as char(3)));

end @

The fuction body can be written in a RETURN statement.

CREATE FUNCTION bigint_to_ip(ip BIGINT)
RETURNS VARCHAR(15)
CONTAINS SQL
DETERMINISTIC
NO EXTERNAL ACTION
RETURN
RTRIM( CHAR( ip / (256*256*256) ) ) || '.' ||
RTRIM( CHAR( MOD(ip , 256*256*256) / (256*256) ) ) || '.' ||
RTRIM( CHAR( MOD(ip , 256*256) / 256 ) ) || '.' ||
CHAR( MOD(ip , 256) )
;

Reply With Quote
  #4  
Old   
brunoalsantos
 
Posts: n/a

Default Re: Converting integer -> IP Address - 09-27-2010 , 02:00 PM



On 27 set, 15:29, Lennart Jonsson <erik.lennart.jons... (AT) gmail (DOT) com>
wrote:
Quote:
On 2010-09-27 19:30, brunoalsantos wrote:

Hello all.

DB2 LUW V9.5 Fp5

Anyone knows a way to transform a integer in a ip address using Scalar
Function?

Is it something like this that you mean?

create function bigint_to_ip (ip bigint)
returns varchar(15)
begin atomic

* * declare fst bigint;
* * declare snd bigint;
* * declare trd bigint;
* * declare fth bigint;

* * set fst = ip / (256*256*256);
* * set snd = mod(ip,256*256*256) / (256*256);
* * set trd = mod(ip,256*256) / 256;
* * set fth = mod(ip,256);

* * return rtrim(cast(fst as char(3))) || '.' ||
* * * * * *rtrim(cast(snd as char(3))) || '.' ||
* * * * * *rtrim(cast(trd as char(3))) || '.' ||
* * * * * *rtrim(cast(fth as char(3)));

end @

db2 "values bigint_to_ip(3555336315)"

1
---------------
211.234.32.123

/Lennart



Regards

Bruno.- Ocultar texto das mensagens anteriores -

- Mostrar texto das mensagens anteriores -
Thanks Lennart.

Reply With Quote
  #5  
Old   
Tonkuma
 
Posts: n/a

Default Re: Converting integer -> IP Address - 09-27-2010 , 02:04 PM



If you upgraded to DB2 LUW V9.7,
the expression could be written a little shorter by using
VARCHAR(bigint) built-in function, like this:

CREATE FUNCTION bigint_to_ip_v97(ip BIGINT)
RETURNS VARCHAR(15)
CONTAINS SQL
DETERMINISTIC
NO EXTERNAL ACTION
RETURN
VARCHAR( ip / (256*256*256) ) || '.' ||
VARCHAR( MOD(ip , 256*256*256) / (256*256) ) || '.' ||
VARCHAR( MOD(ip , 256*256) / 256 ) || '.' ||
VARCHAR( MOD(ip , 256) )
;

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.