Skip to content

Instantly share code, notes, and snippets.

@spatialtime
Last active June 8, 2020 23:14
Show Gist options
  • Save spatialtime/2b82ca796283775f230e206d39038392 to your computer and use it in GitHub Desktop.
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.
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