dbTalk Databases Forums  

Ordinal Function

comp.databases.oracle comp.databases.oracle


Discuss Ordinal Function in the comp.databases.oracle forum.



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

Default Ordinal Function - 07-20-2004 , 02:33 PM






Has anyone created a user function to return the ordinal position based
on numeric input?

In other words, f(1) returns 'st', f(2) returns 'nd', f(3) returns
'rd', etc.


Reply With Quote
  #2  
Old   
sybrandb@yahoo.com
 
Posts: n/a

Default Re: Ordinal Function - 07-21-2004 , 03:56 AM






Terry Coccoli <request (AT) ifneeded (DOT) com> wrote

Quote:
Has anyone created a user function to return the ordinal position based
on numeric input?

In other words, f(1) returns 'st', f(2) returns 'nd', f(3) returns
'rd', etc.
Check out the format mask of the to_char function in the sql reference manual.
Other than that (if the format mask is lacking), visit http://asktom.oracle.com
You'll probably find code there.

Sybrand Bakker
Senior Oracle DBA


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

Default Re: Ordinal Function - 07-21-2004 , 10:18 AM



Terry Coccoli <request (AT) ifneeded (DOT) com> wrote

Quote:
Has anyone created a user function to return the ordinal position based
on numeric input?

In other words, f(1) returns 'st', f(2) returns 'nd', f(3) returns
'rd', etc.
This should work over all integers...

CREATE OR REPLACE
FUNCTION F(N IN NUMBER) RETURN VARCHAR2 IS
X_RESULT VARCHAR2(2);
BEGIN
IF ABS(N) BETWEEN 10 AND 20 THEN
RETURN 'th';
END IF;
SELECT DECODE(MOD(ABS(N),10),1,'st', 2, 'nd', 3, 'rd', 'th')
INTO X_RESULT FROM DUAL;
RETURN X_RESULT;

END;


Reply With Quote
  #4  
Old   
Mark D Powell
 
Posts: n/a

Default Re: Ordinal Function - 07-21-2004 , 10:26 AM



Terry Coccoli <request (AT) ifneeded (DOT) com> wrote

Quote:
Has anyone created a user function to return the ordinal position based
on numeric input?

In other words, f(1) returns 'st', f(2) returns 'nd', f(3) returns
'rd', etc.
Terry, Ordinal position of what? A varchar2 array? The ASCII value?

-- Mark D Powell --


Reply With Quote
  #5  
Old   
Mark D Powell
 
Posts: n/a

Default Re: Ordinal Function - 07-22-2004 , 10:32 AM



Mark.Powell (AT) eds (DOT) com (Mark D Powell) wrote in message news:<2687bb95.0407210726.2e13c8ec (AT) posting (DOT) google.com>...
Quote:
Terry Coccoli <request (AT) ifneeded (DOT) com> wrote

Has anyone created a user function to return the ordinal position based
on numeric input?

In other words, f(1) returns 'st', f(2) returns 'nd', f(3) returns
'rd', etc.

Terry, Ordinal position of what? A varchar2 array? The ASCII value?

-- Mark D Powell --
No sooner than I hit post did I realsize that you wanted first,
second, third as in 1st, 2nd, 3rd, etc..., but I use google to access
the newsgroup so I had to wait for the index to be updated to show my
post to add on and by afternoon I had work to do.

You can build a function using the following:

select to_char(trunc(sysdate,'MM') + rownum - 1,'fm DDTH'), rownum
from dba_objects
where rownum < 32
order by 2

Which will produce something like
TO_CH ROWNUM
----- ----------
1ST 1
2ND 2
3RD 3
4TH 4
5TH 5
6TH 6
7TH 7
8TH 8
9TH 9
10TH 10
11TH 11
12TH 12
13TH 13
14TH 14
....

HTH -- Mark D Powell --


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.