Ordinal Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Terry Coccoli

    Ordinal Function

    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.

  • sybrandb@yahoo.com

    #2
    Re: Ordinal Function

    Terry Coccoli <request@ifneed ed.comwrote in message news:<gseLc.200 93110$Id.332311 2@news.easynews .com>...
    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

    Comment

    • Kevin

      #3
      Re: Ordinal Function

      Terry Coccoli <request@ifneed ed.comwrote in message news:<gseLc.200 93110$Id.332311 2@news.easynews .com>...
      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;

      Comment

      • Mark D Powell

        #4
        Re: Ordinal Function

        Terry Coccoli <request@ifneed ed.comwrote in message news:<gseLc.200 93110$Id.332311 2@news.easynews .com>...
        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 --

        Comment

        Working...