MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help Regarding SUBSTR function...

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
satheeshs
Beginner


Joined: 06 Jul 2006
Posts: 13
Topics: 3
Location: 'Mainframe World'

PostPosted: Thu Jul 06, 2006 8:31 am    Post subject: Help Regarding SUBSTR function... Reply with quote

hi all,
i need help regarding SUBSTR function.
i want to select a column in a db2 table which is Decimal(11,0) in the way
that last 2 characters to be at first and 1st 9 characters concatenated with that.

i tried SUBSTR(char(var1),9,2) concat SUBSTR(char(var1),1,9), but it is not giving the expected result!

Please help me out in this!
_________________
Satheesh
Back to top
View user's profile Send private message Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Thu Jul 06, 2006 8:40 am    Post subject: Reply with quote

satheeshs,

Is this DB2 question or a REXX question ? If it is a DB2 question it must be posted in the Database forum.
Quote:

that last 2 characters to be at first and 1st 9 characters concatenated with that.

i tried SUBSTR(char(var1),9,2) concat SUBSTR(char(var1),1,9), but it is not giving the expected result!


A column defined as decimal (11,0) is 11 bytes long and if you want the last 2 digits in the beginning then you should start from 10th byte. You are starting from 9th byte for length of 2 which would take 9 and 10th byte, which is the reason for your incorrect results.

try this
Code:
SUBSTR(char(var1),10,2) concat
SUBSTR(char(var1),01,9)


Hope this helps...

Cheers

Kolusu

Ps: Please clarify if this question is indeed a Db2 Question , so that I can move it to the approriate forum.
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
satheeshs
Beginner


Joined: 06 Jul 2006
Posts: 13
Topics: 3
Location: 'Mainframe World'

PostPosted: Thu Jul 06, 2006 8:50 am    Post subject: Reply with quote

Thanks all,

i apologize for posted this question in this forum. i am very new to this forum.

i tried with SUBSTR(char(var1),10,2) concat SUBSTR(char(var1),01,9)
but still is not giving the expected result

what i have tried :
47302899954 is the value in the table of column which is of type DECIMAL(11,0)

When i have given the query SELECT SUBSTR(char(var1),10,2) concat SUBSTR(char(var1),01,9) from x.table;

it had given me
95 47302899

(with the space after the '95')

P.S:if possible move it to DB" forum. Thanks . Sorry again!
_________________
Satheesh
Back to top
View user's profile Send private message Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Thu Jul 06, 2006 8:56 am    Post subject: Reply with quote

Quote:

When i have given the query SELECT SUBSTR(char(var1),10,2) concat SUBSTR(char(var1),01,9) from x.table;

it had given me
95 47302899

(with the space after the '95')


satheeshs,

That is only Spufi's Display putting a space between them. Try unloading the data using the same query and you will not see the space in between.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
satheeshs
Beginner


Joined: 06 Jul 2006
Posts: 13
Topics: 3
Location: 'Mainframe World'

PostPosted: Thu Jul 06, 2006 9:03 am    Post subject: Reply with quote

putting a space in between is fine, but a number got truncated.

The expected result should be
54 473028999

but it is coming as
95 47302899

that's why i feel doubt about the space between the numbers
_________________
Satheesh
Back to top
View user's profile Send private message Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Thu Jul 06, 2006 9:16 am    Post subject: Reply with quote

Quote:

putting a space in between is fine, but a number got truncated.


That is because the CHAR function reserves the leading byte for Sign. It puts a space for the positive numbers and '-' for negative numbers. If you don't have any negative numbers then you can use DIGITS function instead of CHAR function.

Code:

SUBSTR(DIGITS(ID_NO),10,2) ||
SUBSTR(DIGITS(ID_NO),01,9)   


How do you wanna handle negative numbers?

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
satheeshs
Beginner


Joined: 06 Jul 2006
Posts: 13
Topics: 3
Location: 'Mainframe World'

PostPosted: Thu Jul 06, 2006 9:23 am    Post subject: Reply with quote

Thanks kolusu.

it works. in my table, it has only positive numbers. so no probs.
if possible could you tell me to handle negative numbers, i could learn!
_________________
Satheesh
Back to top
View user's profile Send private message Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Thu Jul 06, 2006 9:36 am    Post subject: Reply with quote

Quote:

if possible could you tell me to handle negative numbers, i could learn!

satheeshs,

Where do you want the negative sign ?

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
satheeshs
Beginner


Joined: 06 Jul 2006
Posts: 13
Topics: 3
Location: 'Mainframe World'

PostPosted: Thu Jul 06, 2006 9:43 am    Post subject: Reply with quote

in the first plaec itself.

say -4578
should come as
-7845
_________________
Satheesh
Back to top
View user's profile Send private message Yahoo Messenger
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12376
Topics: 75
Location: San Jose

PostPosted: Thu Jul 06, 2006 9:45 am    Post subject: Reply with quote

satheeshs,

Try this

Code:


SELECT CASE WHEN Dec_Col < 0 THEN           
            CHAR('-')                    || 
            SUBSTR(DIGITS(Dec_Col),10,2) || 
            SUBSTR(DIGITS(Dec_Col),01,9)     
       ELSE                               
            CHAR('+')                    || 
            SUBSTR(DIGITS(Dec_Col),10,2) || 
            SUBSTR(DIGITS(Dec_Col),01,9)     
       END                                 
  FROM TABLE                                 
  ;         


Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
satheeshs
Beginner


Joined: 06 Jul 2006
Posts: 13
Topics: 3
Location: 'Mainframe World'

PostPosted: Thu Jul 06, 2006 9:48 am    Post subject: Reply with quote

Thanks kolusu
_________________
Satheesh
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group