Oracle兼容-函数-CURRENT_TIMESTAMP()函数


1. 语法

  1. CURRENT_TIMESTAMP[([n])]

2. 定义和用法

返回当前时间戳。

参数 n 说明:

  1. n的值要小于等于6。
  2. 在ORACLE mode中,CURRENT_TIMESTAMP 等价于 CURRENT_TIMESTAMP(6),小数位是6(n=6),而 CURRENT_TIMESTAMP() 小数位是0(n=0)。而在DEFAULT mode中,CURRENT_TIMESTAMPCURRENT_TIMESTAMP() 等价,小数位都是0(n=0)。
  3. 在ORACLE mode中,输出值格式为dd-mm-yy hh:mm:ss[.dec] am|pm timezone,而在DEFAULT mode中,输出值格式为yyyy-mm-dd hh24:mm:ss[.dec]
  4. 在ORACLE mode中,时间格式与原有的不一样,这会导致部分函数(如UNIX_TIMESTAMP())当其参数值为CURRENT_TIMESTAMP时会显示异常。

3. 示例

  1. greatsql> SET sql_mode = ORACLE;
  2. greatsql> SELECT CURRENT_TIMESTAMP;
  3. +------------------------------------+
  4. | CURRENT_TIMESTAMP |
  5. +------------------------------------+
  6. | 30-10-23 03:53:18.127314 PM +08:00 |
  7. +------------------------------------+
  8. greatsql> SELECT CURRENT_TIMESTAMP();
  9. +-----------------------------+
  10. | CURRENT_TIMESTAMP() |
  11. +-----------------------------+
  12. | 30-10-23 03:53:30 PM +08:00 |
  13. +-----------------------------+
  14. greatsql> SELECT CURRENT_TIMESTAMP(6);
  15. +------------------------------------+
  16. | CURRENT_TIMESTAMP(6) |
  17. +------------------------------------+
  18. | 30-10-23 03:53:41.122988 PM +08:00 |
  19. +------------------------------------+
  20. -- 小数位>6时会报错
  21. greatsql> SELECT CURRENT_TIMESTAMP(7);
  22. ERROR 1426 (42000): Too-big precision 7 specified for 'now'. Maximum is 6.
  23. greatsql> CREATE TABLE t1(
  24. id INT,
  25. c VARCHAR2(64)
  26. );
  27. greatsql> INSERT INTO t1 VALUES(1, CURRENT_TIMESTAMP),
  28. (2, CURRENT_TIMESTAMP()),
  29. (3, CURRENT_TIMESTAMP(3)),
  30. (4, CURRENT_TIMESTAMP(6));
  31. greatsql> SELECT * FROM t1;
  32. +------+------------------------------------+
  33. | id | c |
  34. +------+------------------------------------+
  35. | 1 | 30-10-23 03:55:12.024380 PM +08:00 |
  36. | 2 | 30-10-23 03:55:17 PM +08:00 |
  37. | 3 | 30-10-23 03:55:26.559 PM +08:00 |
  38. | 4 | 30-10-23 03:55:29.372250 PM +08:00 |
  39. +------+------------------------------------+
  40. greatsql> SET sql_mode = DEFAULT;
  41. greatsql> SELECT CURRENT_TIMESTAMP;
  42. +---------------------+
  43. | CURRENT_TIMESTAMP |
  44. +---------------------+
  45. | 2023-10-30 15:55:59 |
  46. +---------------------+
  47. greatsql> SELECT CURRENT_TIMESTAMP();
  48. +---------------------+
  49. | CURRENT_TIMESTAMP() |
  50. +---------------------+
  51. | 2023-10-30 15:56:08 |
  52. +---------------------+
  53. greatsql> SELECT CURRENT_TIMESTAMP(6);
  54. +----------------------------+
  55. | CURRENT_TIMESTAMP(6) |
  56. +----------------------------+
  57. | 2023-10-30 15:56:17.512329 |
  58. +----------------------------+
  59. greatsql> INSERT INTO t1 VALUES(5, CURRENT_TIMESTAMP),
  60. (6, CURRENT_TIMESTAMP()),
  61. (7, CURRENT_TIMESTAMP(3)),
  62. (8, CURRENT_TIMESTAMP(6));
  63. greatsql> SELECT * FROM t1;
  64. +------+------------------------------------+
  65. | id | c |
  66. +------+------------------------------------+
  67. | 1 | 30-10-23 03:55:12.024380 PM +08:00 |
  68. | 2 | 30-10-23 03:55:17 PM +08:00 |
  69. | 3 | 30-10-23 03:55:26.559 PM +08:00 |
  70. | 4 | 30-10-23 03:55:29.372250 PM +08:00 |
  71. | 5 | 2023-10-30 15:56:31 |
  72. | 6 | 2023-10-30 15:56:34 |
  73. | 7 | 2023-10-30 15:56:37.998 |
  74. | 7 | 2023-10-30 15:56:39.494867 |
  75. +------+------------------------------------+

问题反馈

联系我们

扫码关注微信公众号

greatsql-wx