[关闭]
@lumincinta 2017-01-25T12:01:48.000000Z 字数 2669 阅读 331

SQL 日期和时间函数

SQL DB2


SQL 日期和时间函数

函数名 含义
DAYNAME 返回一个大小写混合的字符串,对于参数的日部分,用星期表示这一天的名称(例如,Friday)。
DAYOFWEEK 返回参数中的星期几,用范围在 1-7 的整数值表示,其中 1 代表星期日。
DAYOFWEEK_ISO 返回参数中的星期几,用范围在 1-7 的整数值表示,其中 1 代表星期一。
DAYOFYEAR 返回参数中一年中的第几天,用范围在 1-366 的整数值表示。
DAYS 返回日期的整数表示。
JULIAN_DAY 返回从公元前 4712 年 1 月 1 日(儒略日历的开始日期)到参数中指定日期值之间的天数,用整数值表示。
MIDNIGHT_SECONDS 返回午夜和参数中指定的时间值之间的秒数,用范围在 0 到 86400 之间的整数值表示。
*MONTHNAME 对于参数的月部分的月份,返回一个大小写混合的字符串(例如,January)。
TIMESTAMP_ISO 根据日期、时间或时间戳记参数而返回一个时间戳记值。
TIMESTAMP_FORMAT 从已使用字符模板解释的字符串返回时间戳记。
TIMESTAMPDIFF 根据两个时间戳记之间的时差,返回由第一个参数定义的类型表示的估计时差。
TO_CHAR 返回已用字符模板进行格式化的时间戳记的字符表示。TO_CHARVARCHAR_FORMAT 的同义词。
TO_DATE 从已使用字符模板解释过的字符串返回时间戳记。TO_DATETIMESTAMP_FORMAT 的同义词。
WEEK 返回参数中一年的第几周,用范围在 1-54 的整数值表示。以星期日作为一周的开始。
WEEK_ISO 返回参数中一年的第几周,用范围在 1-53 的整数值表示。

示例

  1. --获取当前日期:
  2. select current date from sysibm.sysdummy1;
  3. values current date;
  4. --获取当前日期
  5. select current time from sysibm.sysdummy1;
  6. values current time;
  7. --获取当前时间戳
  8. select current timestamp from sysibm.sysdummy1;
  9. values current timestamp;
  10. --要使当前时间或当前时间戳记调整到 GMT/CUT,则把当前的时间或时间戳记减去当前时区寄存器:
  11. values current time -current timezone;
  12. values current timestamp -current timezone;
  13. --获取当前年份
  14. values year(current timestamp);
  15. --获取当前月
  16. values month(current timestamp);
  17. --获取当前日
  18. values day(current timestamp);
  19. --获取当前时
  20. values hour(current timestamp);
  21. --获取分钟
  22. values minute(current timestamp);
  23. --获取秒
  24. values second(current timestamp);
  25. --获取毫秒
  26. values microsecond(current timestamp);
  27. --从时间戳记单独抽取出日期和时间
  28. values date(current timestamp);
  29. values VARCHAR_FORMAT(current TIMESTAMP,'yyyy-mm-dd');
  30. values char(current date);
  31. values time(current timestamp);
  32. --执行日期和时间的计算
  33. values current date+1 year;
  34. values current date+3 years+2 months +15 days;
  35. values current time +5 hours -3 minutes +10 seconds;
  36. --计算两个日期之间的天数
  37. values days(current date)- days(date('2010-02-20'));
  38. --时间和日期换成字符串
  39. values char(current date);
  40. values char(current time);
  41. --要将字符串转换成日期或时间值
  42. values timestamp('2010-03-09-22.43.00.000000');
  43. values timestamp('2010-03-09 22:44:36');
  44. values date('2010-03-09');
  45. values date('03/09/2010');
  46. values time('22:45:27');
  47. values time('22.45.27');

计算两个时间戳之间的差

  1. --秒的小数部分为单位
  2. values timestampdiff(1,char(current timestamp - timestamp('2010-01-01-00.00.00')));
  3. --秒为单位
  4. values timestampdiff(2,char(current timestamp - timestamp('2010-01-01-00.00.00')));
  5. --分为单位
  6. values timestampdiff(4,char(current timestamp - timestamp('2010-01-01-00.00.00')));
  7. --小时为单位
  8. values timestampdiff(8,char(current timestamp - timestamp('2010-01-01-00.00.00')));
  9. --天为单位
  10. values timestampdiff(16,char(current timestamp - timestamp('2010-01-01-00.00.00')));
  11. --周为单位
  12. values timestampdiff(32,char(current timestamp - timestamp('2010-01-01-00.00.00')));
  13. --月为单位
  14. values timestampdiff(64,char(current timestamp - timestamp('2010-01-01-00.00.00')));
  15. --季度为单位
  16. values timestampdiff(128,char(current timestamp - timestamp('2010-01-01-00.00.00')));
  17. --年为单位
  18. values timestampdiff(256,char(current timestamp - timestamp('2010-01-01-00.00.00')));
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注