DB2 小技巧

  1. 取時間點的年份的寫法:
    SELECT YEAR(current timestamp) FROM SYSIBM.SYSDUMMY1;
  2. 取時間點的月份的寫法:
    SELECT MONTH(current timestamp) FROM SYSIBM.SYSDUMMY1;
  3. 取時間點的日的寫法:
    SELECT DAY(current timestamp) FROM SYSIBM.SYSDUMMY1;
  4. 取時間點的時的寫法:
    SELECT HOUR(current timestamp) FROM SYSIBM.SYSDUMMY1;
  5. 取時間點的分的寫法:
    SELECT MINUTE(current timestamp) FROM SYSIBM.SYSDUMMY1;
  6. 取時間點的秒的寫法:
    SELECT SECOND(current timestamp) FROM SYSIBM.SYSDUMMY1;
  7. 取時間點的毫秒的寫法:
    SELECT MICROSECOND(current timestamp) FROM SYSIBM.SYSDUMMY1;
  8. 取時間點的日期的寫法:
    SELECT DATE(current timestamp) FROM SYSIBM.SYSDUMMY1;
  9. 取時間點的時間的寫法:
    SELECT TIME(current timestamp) FROM SYSIBM.SYSDUMMY1;
  10. 日期,時間形態變為字符形態:
    SELECT char(current date) FROM SYSIBM.SYSDUMMY1;
    SELECT char(current time) FROM SYSIBM.SYSDUMMY1;
    SELECT char(current date+12 hours) FROM SYSIBM.SYSDUMMY1;
  11. 將字符串轉換成日期或時間形態:
    SELECT TIMESTAMP('2002-10-20-12.00.00.000000') FROM SYSIBM.SYSDUMMY1;
    SELECT TIMESTAMP('2002-10-20 12:00:00') FROM SYSIBM.SYSDUMMY1;
    SELECT DATE('2002-10-20') FROM SYSIBM.SYSDUMMY1;
    SELECT DATE('10/20/2002') FROM SYSIBM.SYSDUMMY1;
    SELECT TIME('12:00:00') FROM SYSIBM.SYSDUMMY1;
    SELECT TIME ('12.00.00') FROM SYSIBM.SYSDUMMY1;
  12. 返回參數的星期几的寫法:
    SELECT DAYNAME(current timestamp) FROM SYSIBM.SYSDUMMY1;
    SELECT DAYOFWEEK(current timestamp) FROM SYSIBM.SYSDUMMY1;
    SELECT DAYOFWEEK_ISO(current timestamp) FROM SYSIBM.SYSDUMMY1;
  13. 返回參數一年中的第几天的寫法:
    SELECT DAYOFYEAR(current timestamp) FROM SYSIBM.SYSDUMMY1;
  14. 返回午夜和參數中指定的時間值之間的秒數的寫法:
    SELECT MIDNIGHT_SECONDS(current timestamp) FORM SYSIBM.SYSDUMMY1;
  15. 返回參數中一年的第几周的寫法:
    SELECT WEEK(current timestamp) FORM SYSIBM.SYSDUMMY1