If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > SQL Built in FUNCTIONS

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-21-04, 10:56
dipsangel dipsangel is offline
Registered User
 
Join Date: Apr 2004
Posts: 22
Exclamation SQL Built in FUNCTIONS

I have a column called deptno VARCHAR2(10)
it has all the records of length varying from 1 to 5
I need to display last two digits only. If the record is only one digit,
it should get lpadded with one zero to make it two digits.

For e.g.

Deptno
---------
1
450
23
5587
56540
5

should be displayed as

Deptno
-----------
01
50
23
87
40
05

Can any one help in this?? Do I need an IF like statement in SELECT ?? I dont really know how to begin with !

Thanks much
Reply With Quote
  #2 (permalink)  
Old 05-21-04, 11:07
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,415
>Do I need an IF like statement in SELECT ??
"IF" is not a valid SQL construct.
It is past time for you to RTFM
http://download-west.oracle.com/docs...ns2a.htm#80856
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #3 (permalink)  
Old 05-21-04, 11:25
dipsangel dipsangel is offline
Registered User
 
Join Date: Apr 2004
Posts: 22
Thumbs up

Quote:
Originally Posted by anacedent
>Do I need an IF like statement in SELECT ??
"IF" is not a valid SQL construct.
It is past time for you to RTFM
http://download-west.oracle.com/docs...ns2a.htm#80856

---------------------

Thanks for the link. I am sure there is a way to do it.
Would get back to you, once i figure that out. KNOWLEDGE IS TO SHARE !
Reply With Quote
  #4 (permalink)  
Old 05-21-04, 11:27
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,415
Start small/simple.
How do you extract the rightmost two digits/characters from a longer value?
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #5 (permalink)  
Old 05-21-04, 12:05
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,415
Alternatively, you could zero fill on the left side & make all the values the same length. This would simplify the complete solution.
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #6 (permalink)  
Old 05-21-04, 12:34
billm billm is offline
Drunkard
 
Join Date: Nov 2002
Location: Desk, slightly south of keyboard
Posts: 697
Code:
select substr('0'||deptno,-2)
from yourtable
Hth
Bill
__________________
Please don't email me directly with questions. I've probably just got home from the pub and cannot guarantee the sanity of my answers. In fact, I can't believe I actually made it home.
Reply With Quote
  #7 (permalink)  
Old 05-21-04, 12:36
dipsangel dipsangel is offline
Registered User
 
Join Date: Apr 2004
Posts: 22
Talking

Quote:
Originally Posted by anacedent
Alternatively, you could zero fill on the left side & make all the values the same length. This would simplify the complete solution.
------------------------

AWESOME !!!

YOU MADE IT SO SIMPLE...thank you so much !!!

I was trying something like this...

SELECT
DECODE
(XRNDEPT,
LENGTH(TO_CHAR(XRNDEPT))>2,SUBSTR(XRNDEPT,
LENGTH(TO_CHAR(XRNDEPT))-1,LENGTH(TO_CHAR(XRNDEPT))),
LENGTH(TO_CHAR(XRNDEPT))<2,LPAD(XRNDEPT,2,'0'),
LENGTH(TO_CHAR(XRNDEPT))=2,XRNDEPT)
FROM CNVXREF;
Reply With Quote
  #8 (permalink)  
Old 05-21-04, 12:47
dipsangel dipsangel is offline
Registered User
 
Join Date: Apr 2004
Posts: 22
Talking

Quote:
Originally Posted by billm
Code:
select substr('0'||deptno,-2)
from yourtable
Hth
Bill
------------------

THANKS MAN !!!

SELECT SUBSTR(LPAD(XRNDEPT,3,'0'),2,3)
FROM CNVXREF;

IT WORKED !!!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On