mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
11 KiB
141 lines
11 KiB
3 years ago
|
---
|
||
3 years ago
|
title: "Formulas"
|
||
3 years ago
|
description: "NocoDB Formulas Syntaxes and Functions"
|
||
3 years ago
|
---
|
||
|
|
||
3 years ago
|
## Adding formula column
|
||
|
|
||
1 year ago
|
<img width="990" alt="image" src="https://user-images.githubusercontent.com/35857179/189108950-fba76e31-8ae4-4108-916b-e413c841f451.png" />
|
||
3 years ago
|
|
||
3 years ago
|
### 1. Click on '+' (Add column)
|
||
|
|
||
|
### 2. Populate column Name
|
||
|
|
||
|
### 3. Select column Type as 'Formula'
|
||
|
|
||
|
### 4. Insert required formula
|
||
|
|
||
3 years ago
|
- You can use explicit numerical values/ strings as needed, e.g. `123` (numeric) or `"123"` (string).
|
||
3 years ago
|
- You can reference column names in equation with `{}`, e.g. `{column_name}`, if the column name conflicts with literals
|
||
3 years ago
|
- Table below lists supported formula & associated syntax
|
||
3 years ago
|
- Nested formula (formula equation referring to another formula column) is supported
|
||
3 years ago
|
|
||
|
### 5. Click on 'Save'
|
||
|
|
||
2 years ago
|
## Editing formula column
|
||
|
|
||
1 year ago
|
Unlike other column types, formula cells cannot be modified by double-clicking since the value is generated based on the formula. Instead, the vaule can be changed by updating the formula in the column setting.
|
||
2 years ago
|
|
||
1 year ago
|
<img width="253" alt="image" src="https://user-images.githubusercontent.com/35857179/189109486-4d41f2b7-0a19-46ef-8bb4-a8d1aabd3592.png" />
|
||
2 years ago
|
|
||
3 years ago
|
## Available Formula Features
|
||
|
|
||
3 years ago
|
### Numeric Functions
|
||
|
|
||
3 years ago
|
| Name | Syntax | Sample | Output |
|
||
|
|-------------|----------------------------|----------------------------------|------------------------------------------------------------------|
|
||
3 years ago
|
| **ABS** | `ABS(value)` | `ABS({Column})` | Absolute value of the input parameter |
|
||
|
| **ADD** | `ADD(value1,[value2,...])` | `ADD({Column1}, {Column2})` | Sum of input parameters |
|
||
|
| **AVG** | `AVG(value1,[value2,...])` | `AVG({Column1}, {Column2})` | Average of input parameters |
|
||
|
| **CEILING** | `CEILING(value)` | `CEILING({Column})` | Rounded next largest integer value of input parameter |
|
||
|
| **EXP** | `EXP(value)` | `EXP({Column})` | Exponential value of input parameter (`e^x`) |
|
||
|
| **FLOOR** | `FLOOR(value)` | `FLOOR({Column})` | Rounded largest integer less than or equal to input parameter |
|
||
|
| **INT** | `INT(value)` | `INT({Column})` | Integer value of input parameter |
|
||
|
| **LOG** | `LOG([base], value)` | `LOG(10, {Column})` | Logarithm of input parameter to the base (default = e) specified |
|
||
|
| **MAX** | `MAX(value1,[value2,...])` | `MAX({Column1}, {Column2}, {Column3})` | Maximum value amongst input parameters |
|
||
|
| **MIN** | `MIN(value1,[value2,...])` | `MIN({Column1}, {Column2}, {Column3})` | Minimum value amongst input parameters |
|
||
|
| **MOD** | `MOD(value1, value2)` | `MOD({Column}, 2)` | Remainder after integer division of input parameters |
|
||
|
| **POWER** | `POWER(base, exponent)` | `POWER({Column}, 3)` | `base` to the `exponent` power, as in `base ^ exponent` |
|
||
2 years ago
|
| **ROUND** | `ROUND(value, precision)` | `ROUND({Column}, 3)` | Round input `value` to decimal place specified by `precision` (Nearest integer if `precision` not provided) |
|
||
3 years ago
|
| **SQRT** | `SQRT(value)` | `SQRT({Column})` | Square root of the input parameter |
|
||
3 years ago
|
|
||
3 years ago
|
|
||
|
### Numeric Operators
|
||
|
|
||
3 years ago
|
| Operator | Sample | Description |
|
||
|
| -------- | ----------------------- | -------------------------------- |
|
||
3 years ago
|
| `+` | `{Column1} + {Column2} + 2` | Addition of numeric values |
|
||
|
| `-` | `{Column1} - {Column2}` | Subtraction of numeric values |
|
||
|
| `*` | `{Column1} * {Column2}` | Multiplication of numeric values |
|
||
|
| `/` | `{Column1} / {Column2}` | Division of numeric values |
|
||
3 years ago
|
|
||
1 year ago
|
:::tip
|
||
|
|
||
|
To change the order of arithmetic operation, you can use round bracket parantheses (). <br/>
|
||
|
Example: ({Column1} + ({Column2} * {Column3}) / (3 - $Column4$ ))
|
||
|
|
||
|
:::
|
||
3 years ago
|
|
||
|
### String Functions
|
||
|
|
||
3 years ago
|
| Name | Syntax | Sample | Output |
|
||
|
|-------------|----------------------------------|---------------------------------|---------------------------------------------------------------------------|
|
||
3 years ago
|
| **CONCAT** | `CONCAT(str1, [str2,...])` | `CONCAT({Column1}, ' ', {Column2})` | Concatenated string of input parameters |
|
||
3 years ago
|
| **LEFT** | `LEFT(str1, n)` | `LEFT({Column}, 3)` | `n` characters from the beginning of input parameter |
|
||
3 years ago
|
| **LEN** | `LEN(str)` | `LEN({Column})` | Input parameter character length |
|
||
|
| **LOWER** | `LOWER(str)` | `LOWER({Column})` | Lower case converted string of input parameter |
|
||
3 years ago
|
| **MID** | `MID(str, position, [count])` | `MID({Column}, 3, 2)` | Alias for `SUBSTR` |
|
||
3 years ago
|
| **REPEAT** | `REPEAT(str, count)` | `REPEAT({Column}, 2)` | Specified copies of the input parameter string concatenated together |
|
||
|
| **REPLACE** | `REPLACE(str, srchStr, rplcStr)` | `REPLACE({Column}, 'int', 'num')` | String, after replacing all occurrences of `srchStr` with `rplcStr` |
|
||
3 years ago
|
| **RIGHT** | `RIGHT(str, n)` | `RIGHT({Column}, 3)` | `n` characters from the end of input parameter |
|
||
3 years ago
|
| **SEARCH** | `SEARCH(str, srchStr)` | `SEARCH({Column}, 'str')` | Index of `srchStr` specified if found, 0 otherwise |
|
||
|
| **SUBSTR** | `SUBTR(str, position, [count])` | `SUBSTR({Column}, 3, 2)` | Substring of length 'count' of input string, from the postition specified |
|
||
|
| **TRIM** | `TRIM(str)` | `TRIM({Column})` | Remove trailing and leading whitespaces from input parameter |
|
||
|
| **UPPER** | `UPPER(str)` | `UPPER({Column})` | Upper case converted string of input parameter |
|
||
|
| **URL** | `URL(str)` | `URL({Column})` | Convert to a hyperlink if it is a valid URL |
|
||
3 years ago
|
|
||
3 years ago
|
### Date Functions
|
||
|
|
||
3 years ago
|
| Name | Syntax | Sample | Output | Remark |
|
||
|
|---|---|---|---|---|
|
||
3 years ago
|
| **NOW** | `NOW()` | `NOW()` | 2022-05-19 17:20:43 | Returns the current time and day |
|
||
3 years ago
|
| | `IF(NOW() < {DATE_COL}, "true", "false")` | `IF(NOW() < date, "true", "false")` | If current date is less than {DATE_COL}, it returns true. Otherwise, it returns false. | DateTime columns and negative values are supported. |
|
||
3 years ago
|
| **DATEADD** | `DATEADD(date \| datetime, value, ["day" \| "week" \| "month" \| "year"])` | `DATEADD(date, 1, 'day')` | Supposing {DATE_COL} is 2022-03-14. The result is 2022-03-15. | DateTime columns and negative values are supported. Example: `DATEADD(DATE_TIME_COL, -1, 'day')` |
|
||
|
| | | `DATEADD(date, 1, 'week')` | Supposing {DATE_COL} is 2022-03-14 03:14. The result is 2022-03-21 03:14. | DateTime columns and negative values are supported. Example: `DATEADD(DATE_TIME_COL, -1, 'week')` |
|
||
|
| | | `DATEADD(date, 1, 'month')` | Supposing {DATE_COL} is 2022-03-14 03:14. The result is 2022-04-14 03:14. | DateTime columns and negative values are supported. Example: `DATEADD(DATE_TIME_COL, -1, 'month')` |
|
||
|
| | | `DATEADD(date, 1, 'year')` | Supposing {DATE_COL} is 2022-03-14 03:14. The result is 2023-03-14 03:14. | DateTime columns and negative values are supported. Example: `DATEADD(DATE_TIME_COL, -1, 'year')` |
|
||
|
| | | `IF(NOW() < DATEADD(date,10,'day'), "true", "false")` | If the current date is less than {DATE_COL} plus 10 days, it returns true. Otherwise, it returns false. | DateTime columns and negative values are supported. |
|
||
2 years ago
|
| | | `IF(NOW() < DATEADD(date,10,'day'), "true", "false")` | If the current date is less than {DATE_COL} plus 10 days, it returns true. Otherwise, it returns false. | DateTime columns and negative values are supported. |
|
||
2 years ago
|
| **DATETIME_DIFF** | `DATETIME_DIFF(date, date, ["milliseconds" \| "ms" \| "seconds" \| "s" \| "minutes" \| "m" \| "hours" \| "h" \| "days" \| "d" \| "weeks" \| "w" \| "months" \| "M" \| "quarters" \| "Q" \| "years" \| "y"])` | `DATETIME_DIFF("2022/10/14", "2022/10/15", "second")` | Supposing {DATE_COL_1} is 2017-08-25 and {DATE_COL_2} is 2011-08-25. The result is 86400. | Compares two dates and returns the difference in the unit specified. Positive integers indicate the second date being in the past compared to the first and vice versa for negative ones. |
|
||
|
| | | `WEEKDAY(NOW(), "sunday")` | If today is Monday, it returns 1 | Get the week day of NOW() with the first day set as sunday |
|
||
2 years ago
|
| **WEEKDAY** | `WEEKDAY(date, [startDayOfWeek])` | `WEEKDAY(NOW())` | If today is Monday, it returns 0 | Returns the day of the week as an integer between 0 and 6 inclusive starting from Monday by default. You can optionally change the start day of the week by specifying in the second argument |
|
||
|
| | | `WEEKDAY(NOW(), "sunday")` | If today is Monday, it returns 1 | Get the week day of NOW() with the first day set as sunday |
|
||
|
|
||
3 years ago
|
### Logical Operators
|
||
3 years ago
|
|
||
3 years ago
|
| Operator | Sample | Description |
|
||
|
| -------- | -------------------- | ------------------------ |
|
||
3 years ago
|
| `<` | `{Column1} < {Column2}` | Less than |
|
||
|
| `>` | `{Column1} > {Column2}` | Greater than |
|
||
|
| `<=` | `{Column1} <= {Column2}` | Less than or equal to |
|
||
|
| `>=` | `{Column1} >= {Column2}` | Greater than or equal to |
|
||
|
| `==` | `{Column1} == {Column2}` | Equal to |
|
||
|
| `!=` | `{Column1} != {Column2}` | Not equal to |
|
||
3 years ago
|
|
||
|
|
||
|
### Conditional Expressions
|
||
3 years ago
|
|
||
|
| Name | Syntax | Sample | Output |
|
||
|
|------------|------------------------------------------------|---------------------------------------------|-------------------------------------------------------------|
|
||
3 years ago
|
| **IF** | `IF(expr, successCase, elseCase)` | `IF({Column} > 1, Value1, Value2)` | successCase if `expr` evaluates to TRUE, elseCase otherwise |
|
||
3 years ago
|
| **SWITCH** | `SWITCH(expr, [pattern, value, ..., default])` | `SWITCH({Column}, 1, 'One', 2, 'Two', '--')` | Switch case value based on `expr` output |
|
||
|
| **AND** | `AND(expr1, [expr2,...])` | `AND({Column} > 2, {Column} < 10)` | TRUE if all `expr` evaluate to TRUE |
|
||
|
| **OR** | `OR(expr1, [expr2,...])` | `OR({Column} > 2, {Column} < 10)` | TRUE if at least one `expr` evaluates to TRUE |
|
||
3 years ago
|
|
||
|
Logical operators, along with Numerical operators can be used to build conditional `expressions`.
|
||
|
|
||
|
Examples:
|
||
|
|
||
3 years ago
|
```
|
||
3 years ago
|
IF({marksSecured} > 80, "GradeA", "GradeB")
|
||
3 years ago
|
```
|
||
|
|
||
3 years ago
|
```
|
||
3 years ago
|
SWITCH({quarterNumber},
|
||
3 years ago
|
1, 'Jan-Mar',
|
||
|
2, 'Apr-Jun',
|
||
|
3, 'Jul-Sep',
|
||
|
4, 'Oct-Dec',
|
||
|
'INVALID'
|
||
|
)
|
||
3 years ago
|
```
|