§ Oracle Compatibility - Syntax - DATETIME Arithmetic
§ 1. Syntax
SELECT SYSDATE + 1 FROM DUAL;
SELECT SYSDATE - 1 FROM DUAL;
SELECT SYSDATE - SYSDATE FROM DUAL;
1
2
3
2
3
§ 2. Definition and Usage
GreatSQL supports arithmetic operations on DATETIME data, such as adding 1 second or 1 day. To use this operation behavior mode, you need to switch to ORACLE mode.
- Only addition and subtraction are supported; other operation types are not supported.
- Before arithmetic operations on the
TIMEtype, it is first converted to theDATETIMEtype. - The timestamp value is first converted to a date value, and the constants in
NUMBERarithmetic date-time and interval expressions are interpreted as days. - The year range is [0,9999]. Years before the common era (BC) are not supported. When an overflow occurs, NULL is returned with a warning.
- When the calculation result contains an infinite repeating decimal, only 10 digits are kept by default, that is: default value of
div_precincrementoption (4) + 6 = 10. - When performing arithmetic on the YEAR type, the default conversion fails, returning NULL with a warning.
greatsql> SET sql_mode = ORACLE;
-- Example 1: When merging multiple query result sets, or when a value is used as a query condition
greatsql> SELECT TO_DATE('00:00:00','HH24:MI:SS') - TO_DATE('00:00:01','HH24:MI:SS') FROM DUAL
UNION ALL
SELECT TO_DATE('00:00:00','HH24:MI:SS') - TO_DATE('00:00:01','HH24:MI:SS') FROM DUAL;
+----------------------------------------------------------------------+
| to_date('00:00:00','HH24:MI:SS') - to_date('00:00:01','HH24:MI:SS') |
+----------------------------------------------------------------------+
| -0.0000115741 |
| -0.0000115741 |
+----------------------------------------------------------------------+
- Example 2: When a single query calculation result is displayed as a string, the number of retained decimal places is no longer limited to 10
greatsql> SELECT TO_DATE('00:00:00','HH24:MI:SS') - TO_DATE('00:00:01','HH24:MI:SS');
+----------------------------------------------------------------------+
| to_date('00:00:00','HH24:MI:SS') - to_date('00:00:01','HH24:MI:SS') |
+----------------------------------------------------------------------+
| -0.000011574074074074 |
+----------------------------------------------------------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DATETIME type arithmetic compatibility matrix
| Operand & Operator | DATETIME | TIMESTAMP | TIME | NUMBER |
|---|---|---|---|---|
| DATETIME | - | - | - | - |
| + | - | - | - | DATETIME |
| - | DECIMAL | DECIMAL | DECIMAL | DATETIME |
| * | - | - | - | - |
| / | - | - | - | - |
| TIMESTAMP | - | - | - | - |
| + | - | - | - | TIMESTAMP |
| - | DECIMAL | DECIMAL | DECIMAL | TIMESTAMP |
| * | - | - | - | - |
| / | - | - | - | - |
| DATE | ||||
| + | - | - | - | DATETIME |
| - | DECIMAL | DECIMAL | DECIMAL | DATETIME |
| * | - | - | - | - |
| / | - | - | - | - |
| TIME | - | - | - | - |
| + | - | - | - | DATETIME |
| - | DECIMAL | DECIMAL | DECIMAL | DATETIME |
| * | - | - | - | - |
| / | - | - | - | - |
| NUMBER | - | - | - | - |
| + | DATETIME | TIMESTAMP | DATETIME | NA |
| - | DATETIME | TIMESTAMP | DATETIME | NA |
| * | - | - | - | NA |
| / | - | - | - | NA |
The table above is the arithmetic compatibility matrix for the DATETIME type, where the dash represents unsupported operations.
§ 3. Differences from Oracle
- The default precision of the date difference result is inconsistent. In GreatSQL, 10 decimal places are kept by default, while in Oracle, different precision data is used depending on the case.
- The maximum and minimum time ranges are inconsistent. In GreatSQL, the year range is [0,9999], while in Oracle the range is [-4713,9999].
- The behavior of string arithmetic differs. For example, for the string 'a', the conversion fails with an error in Oracle, while in GreatSQL it is converted to '0' with a warning before the operation is performed.
- In GreatSQL,
TIMESTAMP - TIMESTAMPreturns aDECIMALtype, while in Oracle it returns anINTERVALtype.
§ 4. Examples
greatsql> SET sql_mode = ORACLE;
greatsql> SET TIMESTAMP = 1;
-- +1/2 is equivalent to +12 hours
greatsql> SELECT SYSDATE +1/2 ;
+----------------------------+
| SYSDATE +1/2 |
+----------------------------+
| 1970-01-01 12:00:01.000000 |
+----------------------------+
-- Equivalent to +1 day
greatsql> SELECT SYSDATE +1 ;
+----------------------------+
| SYSDATE +1 |
+----------------------------+
| 1970-01-02 00:00:01.000000 |
+----------------------------+
-- Equivalent to -1 day
greatsql> SELECT SYSDATE -1 ;
+----------------------------+
| SYSDATE -1 |
+----------------------------+
| 1969-12-31 00:00:01.000000 |
+----------------------------+
greatsql> SELECT SYSDATE - SYSDATE;
+-------------------+
| SYSDATE - SYSDATE |
+-------------------+
| 0 |
+-------------------+
-- 'a' is converted to '0' before the operation, with a warning
greatsql> SELECT SYSDATE + 'a';
+----------------------------+
| SYSDATE + 'a' |
+----------------------------+
| 1970-01-01 00:00:01.000000 |
+----------------------------+
1 row in set, 1 warning (0.00 sec)
greatsql> SHOW WARNINGS;
+---------+------+---------------------------------------+
| Level | Code | Message |
+---------+------+---------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'a' |
+---------+------+---------------------------------------+
-- Multiplication/division operations are not supported
greatsql> SELECT SYSDATE * 10;
ERROR 7038 (42000): in oracle_mode: date * LONGLONG not allowed
greatsql> SELECT SYSDATE / 10;
ERROR 7038 (42000): in oracle_mode: date / LONGLONG not allowed
greatsql> SELECT SYSDATE + SYSDATE;
ERROR 7038 (42000): in oracle_mode: date + DATETIME not allowed
greatsql> SELECT SYSDATE + 1000000000000;
+-------------------------+
| SYSDATE + 1000000000000 |
+-------------------------+
| NULL |
+-------------------------+
1 row in set, 1 warning (0.00 sec)
greatsql> SHOW WARNINGS;
+---------+------+--------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------+
| Warning | 1441 | Datetime function: datetime field overflow |
+---------+------+--------------------------------------------+
-- Arithmetic cannot be performed on YEAR
greatsql> SELECT CAST(2023 AS YEAR) + 1;
+------------------------+
| CAST(2023 AS YEAR) + 1 |
+------------------------+
| NULL |
+------------------------+
1 row in set, 1 warning (0.00 sec)
greatsql> SHOW WARNINGS;
+---------+------+----------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------+
| Warning | 1292 | Incorrect datetime value: '2023' |
+---------+------+----------------------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Scan the QR code to follow our WeChat official account
