Skip to content

Commit 200047d

Browse files
authored
chore: doc about add_months and column comment (#2449)
1 parent 9a6fe52 commit 200047d

File tree

4 files changed

+107
-6
lines changed

4 files changed

+107
-6
lines changed

docs/en/sql-reference/10-sql-commands/00-ddl/01-table/90-alter-table-column.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sidebar_position: 4
44
---
55
import FunctionDescription from '@site/src/components/FunctionDescription';
66

7-
<FunctionDescription description="Introduced or updated: v1.2.415"/>
7+
<FunctionDescription description="Introduced or updated: v1.2.760"/>
88

99
import EEFeature from '@site/src/components/EEFeature';
1010

@@ -35,13 +35,18 @@ MODIFY [ COLUMN ] <column_name> DROP STORED
3535
ALTER TABLE [ IF EXISTS ] [ <database_name>. ]<table_name>
3636
RENAME [ COLUMN ] <column_name> TO <new_column_name>
3737

38-
-- Change data type and/or comment
39-
-- If you only want to modify or add a comment for a column, you must still specify the current data type for that column in the command
38+
-- Change data type
4039
ALTER TABLE [ IF EXISTS ] [ <database_name>. ]<table_name>
41-
MODIFY [ COLUMN ] <column_name> <new_data_type> [ DEFAULT <constant_value> ] [ COMMENT '<comment>' ]
42-
[ , [ COLUMN ] <column_name> <new_data_type> [ DEFAULT <constant_value> ] [ COMMENT '<comment>' ] ]
40+
MODIFY [ COLUMN ] <column_name> <new_data_type> [ DEFAULT <constant_value> ]
41+
[ , [ COLUMN ] <column_name> <new_data_type> [ DEFAULT <constant_value> ] ]
4342
...
4443

44+
-- Change comment
45+
ALTER TABLE [ IF EXISTS ] [ <database_name>. ]<table_name>
46+
MODIFY [ COLUMN ] <column_name> [ COMMENT '<comment>' ]
47+
[ , [ COLUMN ] <column_name> [ COMMENT '<comment>' ] ]
48+
...
49+
4550
-- Set / Unset masking policy for a column
4651
ALTER TABLE [ IF EXISTS ] [ <database_name>. ]<table_name>
4752
MODIFY [ COLUMN ] <column_name> SET MASKING POLICY <policy_name>
@@ -218,7 +223,7 @@ SHOW CREATE TABLE students_info;
218223
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
219224

220225
-- Add a comment to the 'age' column
221-
ALTER TABLE students_info MODIFY COLUMN age VARCHAR(10) COMMENT 'abc';
226+
ALTER TABLE students_info MODIFY COLUMN age COMMENT 'abc';
222227

223228
SHOW CREATE TABLE students_info;
224229

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: ADD_MONTHS
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.760"/>
7+
8+
The add_months() function adds a specified number of months to a given date or timestamp.
9+
10+
If the input date is month-end or exceeds the resulting month’s days, the result is adjusted to the last day of the new month. Otherwise, the original day is preserved.
11+
12+
## Syntax
13+
14+
```sql
15+
ADD_MONTHS(<date_or_timestamp>, <number_of_months>)
16+
```
17+
18+
| Parameter | Description |
19+
|----------------------|-----------------------------------------------------------------------------|
20+
| `<date_or_timestamp>` | The starting date or timestamp to which months will be added |
21+
| `<number_of_months>` | The integer number of months to add (can be negative to subtract months) |
22+
23+
## Return Type
24+
25+
Returns a TIMESTAMP or DATE type
26+
27+
## Examples
28+
29+
### Basic Month Addition
30+
```sql
31+
SELECT ADD_MONTHS('2023-01-15'::DATE, 3);
32+
├───────────────────────────────────┤
33+
2023-04-15
34+
╰───────────────────────────────────╯
35+
```
36+
37+
### Subtracting Months
38+
```sql
39+
SELECT ADD_MONTHS('2023-06-20'::DATE, -4);
40+
├─────────────────────────────────────┤
41+
2023-02-20
42+
╰─────────────────────────────────────╯
43+
```
44+
45+
### Month-End Adjustment
46+
```sql
47+
SELECT ADD_MONTHS('2023-01-31'::DATE, 1);
48+
├───────────────────────────────────┤
49+
2023-02-28
50+
╰───────────────────────────────────╯
51+
```
52+
53+
### With Timestamp Preservation
54+
```sql
55+
SELECT ADD_MONTHS('2023-03-15 14:30:00'::TIMESTAMP, 5);
56+
├─────────────────────────────────────────────────┤
57+
2023-08-15 14:30:00.000000
58+
╰─────────────────────────────────────────────────╯
59+
```
60+
61+
### With last day of month
62+
```sql
63+
CREATE TABLE contracts (
64+
id INT,
65+
sign_date DATE,
66+
duration_months INT
67+
);
68+
69+
INSERT INTO contracts VALUES
70+
(1, '2023-01-15', 12),
71+
(2, '2024-02-28', 6),
72+
(3, '2023-11-30', 3);
73+
74+
SELECT
75+
id,
76+
sign_date,
77+
ADD_MONTHS(sign_date, duration_months) AS end_date
78+
FROM contracts;
79+
├─────────────────┼────────────────┼────────────────┤
80+
12023-01-152024-01-15
81+
22024-02-282024-08-28
82+
32023-11-302024-02-29
83+
╰───────────────────────────────────────────────────╯
84+
85+
```
86+
87+
## See Also
88+
89+
- [DATE_ADD](date-add.md): Alternative function for adding specific time intervals
90+
- [DATE_SUB](date-sub.md): Function for subtracting time intervals

docs/en/sql-reference/20-sql-functions/05-datetime-functions/date-add.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,8 @@ SELECT DATE_ADD(month, 1, '2023-02-28'::DATE);
9090
╰────────────────────────────────────────╯
9191

9292
```
93+
94+
## See Also
95+
96+
- [ADD_MONTH](add-months.md): Function for add months
97+
- [DATE_SUB](date-sub.md): Function for subtracting time intervals

docs/en/sql-reference/20-sql-functions/05-datetime-functions/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ This page provides a comprehensive overview of Date & Time functions in Databend
5656
| [MONTHS_BETWEEN](months-between.md) | Returns the number of months between two dates | `MONTHS_BETWEEN('2024-06-04', '2024-01-04')``5` |
5757
| [DATE_BETWEEN](date-between.md) | Checks if a date is between two other dates | `DATE_BETWEEN('2024-06-04', '2024-06-01', '2024-06-10')``true` |
5858
| [AGE](age.md) | Calculate the difference between timestamps or between a timestamp and the current date/time | `AGE('2000-01-01'::TIMESTAMP, '1990-05-15'::TIMESTAMP)``9 years 7 months 17 days` |
59+
| [ADD_MONTHS](add-months.md) | Adds months to a date while preserving end-of-month days. | `ADD_MONTHS('2025-04-30',1)``2025-05-31` |
5960

6061
## Date & Time Truncation Functions
6162

0 commit comments

Comments
 (0)