SET search_path = db2,pg_catalog,public;

-- MICROSECOND()
SELECT MICROSECOND('0001-01-01 00:00:00.100000') FROM SYSIBM.SYSDUMMY1;
SELECT MICROSECOND('0001-01-01 12:35:30.100001') FROM SYSIBM.SYSDUMMY1;
SELECT MICROSECOND('0001-01-01 12:35:30.500001') FROM SYSIBM.SYSDUMMY1;

-- SECOND()
SELECT SECOND('0001-01-01 12:35:30.100001') FROM SYSIBM.SYSDUMMY1;
SELECT SECOND('0001-01-01 12:35:30.500001') FROM SYSIBM.SYSDUMMY1;

-- MINUTE()
SELECT MINUTE('0001-01-01 12:35:30.100001') FROM SYSIBM.SYSDUMMY1;
SELECT MINUTE('0001-01-01 12:35:30.100001+02:00') FROM SYSIBM.SYSDUMMY1;

-- HOUR()
SELECT HOUR('0001-01-01 12:35:30.100001') FROM SYSIBM.SYSDUMMY1;
SELECT HOUR('0001-01-01 12:35:30.100001+02:00') FROM SYSIBM.SYSDUMMY1; -- WRONG

-- DAY()
SELECT DAY('0001-01-01 12:35:30.100001') FROM SYSIBM.SYSDUMMY1;
SELECT DAY('1000-01-01') FROM SYSIBM.SYSDUMMY1;
SELECT DAY('0004-02-29') FROM SYSIBM.SYSDUMMY1;

-- YEAR()
SELECT YEAR('0001-01-01') FROM SYSIBM.SYSDUMMY1;

-- DAYS()
SELECT DAYS('1000-01-01') FROM SYSIBM.SYSDUMMY1;
SELECT DAYS('0000-01-01') FROM SYSIBM.SYSDUMMY1; -- ERROR
SELECT DAYS('0001-01-01') FROM SYSIBM.SYSDUMMY1;

-- MONTHS_BETWEEN()
SELECT MONTHS_BETWEEN('0002-01-01', '0001-01-01') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('0002-01-01', '0001-01-02') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('0002-01-01', '0001-01-03') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('0003-01-01', '0001-01-02') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2008-01-17', '2008-02-17') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2008-02-17', '2008-03-17') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2008-02-20', '2008-01-17') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2008-01-17', '2008-02-20') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2008-02-20', '2008-01-20') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2014-07-09', '2014-06-10') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2014-07-09 14:00:00.000000', '2014-06-10') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2014-07-09 14:30:00.000000', '2014-06-10') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2014-07-09 14:30:30.000000', '2014-06-10') FROM SYSIBM.SYSDUMMY1;
SELECT MONTHS_BETWEEN('2014-07-09 14:30:30.000000+06:00', '2014-06-10') FROM SYSIBM.SYSDUMMY1;
SELECT ROUND(MONTHS_BETWEEN('2008-03-29', '2008-02-29')*31,2) FROM SYSIBM.SYSDUMMY1;
SELECT ROUND(MONTHS_BETWEEN('2008-03-30', '2008-02-29')*31,2) FROM SYSIBM.SYSDUMMY1;
SELECT ROUND(MONTHS_BETWEEN('2008-03-31', '2008-02-29')*31,2) FROM SYSIBM.SYSDUMMY1; -- WRONG ("The result 
-- is not 33 because both dates are the last day of their respective month, and so the result is only based 
-- on the year and month portions.")

-- TIMESTAMP_FORMAT()
SELECT timestamp_format('2017-04-16 23:59:59.123456789012', 'YYYY-MM-DD HH24:MI:SS.FF12');
SELECT timestamp_format('2017-04-16 23:59:59.123456789', 'YYYY-MM-DD HH24:MI:SS.FF9');
SELECT timestamp_format('2017-04-16 23:59:59.123456', 'YYYY-MM-DD HH24:MI:SS.FF');
SELECT timestamp_format('2017-04-16 23:59:59.1234', 'YYYY-MM-DD HH24:MI:SS.FF');
SELECT timestamp_format('2017-04-16 23:59:59.12', 'YYYY-MM-DD HH24:MI:SS.FF');
SELECT timestamp_format('2017-04-16 23:59:59.12', 'YYYY-MM-DD HH24:MI:SS');
SELECT timestamp_format('2017-04-16 23:59:59', 'YYYY-MM-DD HH24:MI:SS');
SELECT timestamp_format('2017-May-16 23:59:59', 'YYYY-Mon-DD HH24:MI:SS');
SELECT timestamp_format('2017-02-29 13:00:00', 'YYYY-MM-DD HH24:MI:SS');
