Re: Ordinal Function
From: Kevin <kk2796_at_hotmail.com>
Date: 21 Jul 2004 08:18:11 -0700
Message-ID: <5a1dc659.0407210718.3bfc0b62_at_posting.google.com>
Date: 21 Jul 2004 08:18:11 -0700
Message-ID: <5a1dc659.0407210718.3bfc0b62_at_posting.google.com>
Terry Coccoli <request_at_ifneeded.com> wrote in message news:<gseLc.20093110$Id.3323112_at_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;
Received on Wed Jul 21 2004 - 17:18:11 CEST