Last active
June 8, 2020 23:14
-
-
Save spatialtime/2b82ca796283775f230e206d39038392 to your computer and use it in GitHub Desktop.
A PL/SQL function that formats an Oracle INTERVAL YEAR TO MONTH value to a conformant ISO 8601 string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION format_iso_yminterval(interval_in INTERVAL YEAR TO MONTH) RETURN VARCHAR2 | |
IS | |
iso_out VARCHAR2(14) := 'P'; | |
year NUMBER; | |
month NUMBER; | |
BEGIN | |
year := EXTRACT(YEAR FROM interval_in); | |
IF year <> 0 THEN | |
iso_out := iso_out || year || 'Y'; | |
END IF; | |
month := EXTRACT(MONTH FROM interval_in); | |
IF month <> 0 THEN | |
iso_out := iso_out || month || 'M'; | |
END IF; | |
IF iso_out = 'P' THEN | |
iso_out := iso_out || '0Y0M'; | |
END IF; | |
RETURN iso_out; | |
END; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment