Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for text functions in SQL #1167

Merged
merged 3 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 90 additions & 1 deletion docs/docs/sql-syntax/functions/text/ascii.md
Original file line number Diff line number Diff line change
@@ -1 +1,90 @@
# ASCII
# ASCII

The ASCII function in SQL returns the ASCII value for the first character of the specified string.

## Syntax

The syntax for the ASCII function in SQL is:

```sql
ASCII ( single_character_text )
```

## Parameters

- `single_character_text`: This is the string that the ASCII value should be returned for. It should be a single character string.

## Examples

Let's consider a few examples to understand how to use the ASCII function.

To get the ASCII value of a character:

```sql
VALUES(ASCII('A'));
```

This will return `65`, which is the ASCII value for 'A'.

Please note that the ASCII function expects a single character value. If a string with more than one character is passed, it will throw an error. For instance:

```sql
VALUES(ASCII('AB'));
```

This will throw an error because 'AB' contains more than one character.

You can also use the ASCII function in a SELECT statement. Consider the following table named 'Ascii':

| id | text |
| --- | ---- |
| 1 | 'F' |

```sql
CREATE TABLE Ascii (
id INTEGER,
text TEXT
);
INSERT INTO Ascii VALUES (1, 'F');
```

You can select the ASCII value of the 'text' column:

```sql
SELECT ASCII(text) AS ascii FROM Ascii;
```

This will return `70`, which is the ASCII value for 'F'.

The ASCII function can also take a string directly:

```sql
SELECT ASCII('a') AS ascii FROM Ascii;
```

This will return `97`, which is the ASCII value for 'a'.

If a non-ASCII character is passed to the function, it will throw an error. For instance:

```sql
SELECT ASCII('ㄱ') AS ascii FROM Ascii;
```

This will throw an error because 'ㄱ' is not an ASCII character.

If no argument is passed to the ASCII function, it will also throw an error:

```sql
SELECT ASCII() AS ascii FROM Ascii;
```

This will throw an error because the ASCII function expects one argument.

Remember, the ASCII function expects a single character. If the column value contains more than one character, it will throw an error:

```sql
INSERT INTO Ascii VALUES (1, 'Foo');
SELECT ASCII(text) AS ascii FROM Ascii;
```

This will throw an error because 'Foo' contains more than one character.
83 changes: 82 additions & 1 deletion docs/docs/sql-syntax/functions/text/chr.md
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
# CHR
# CHR

The CHR function in SQL returns the character represented by the specified ASCII value.

## Syntax

The syntax for the CHR function in SQL is:

```sql
CHR ( ascii_value )
```

## Parameters

- `ascii_value`: This is the ASCII value for which the character should be returned. It should be an integer value between 0 and 255.

## Examples

Let's consider a few examples to understand how to use the CHR function.

To get the character for an ASCII value:

```sql
VALUES(CHR(70));
```

This will return `'F'`, which is the character for the ASCII value 70.

Please note that the CHR function expects an integer value between 0 and 255. If a value outside this range is passed, it will throw an error. For instance:

```sql
VALUES(CHR(7070));
```

This will throw an error because 7070 is not a valid ASCII value.

You can also use the CHR function in a SELECT statement. Consider the following table named 'Chr':

| id | num |
| --- | --- |
| 1 | 70 |

```sql
CREATE TABLE Chr (
id INTEGER,
num INTEGER
);
INSERT INTO Chr VALUES (1, 70);
```

You can select the character for the 'num' column:

```sql
SELECT CHR(num) AS chr FROM Chr;
```

This will return `'F'`, which is the character for the ASCII value 70.

The CHR function can also take an integer value directly:

```sql
SELECT CHR(65) AS chr FROM Chr;
```

This will return `'A'`, which is the character for the ASCII value 65.

If a non-integer value is passed to the function, it will throw an error. For instance:

```sql
SELECT CHR('ukjhg') AS chr FROM Chr;
```

This will throw an error because 'ukjhg' is not an integer value.

Remember, the CHR function expects an integer value between 0 and 255. If the column value is outside this range, it will throw an error:

```sql
INSERT INTO Chr VALUES (1, 4345);
SELECT CHR(num) AS chr FROM Chr;
```

This will throw an error because 4345 is not a valid ASCII value.
61 changes: 60 additions & 1 deletion docs/docs/sql-syntax/functions/text/concat-ws.md
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
# CONCAT_WS
# CONCAT_WS

The CONCAT_WS function in SQL concatenates two or more strings into one string with a separator.

## Syntax

The syntax for the CONCAT_WS function in SQL is:

```sql
CONCAT_WS ( separator, string1, string2, ..., stringN )
```

## Parameters

- `separator`: This is the string that will be placed between each string to be concatenated.
- `string1`, `string2`, ..., `stringN`: These are the strings that you wish to concatenate together.

## Examples

Let's consider a few examples to understand how to use the CONCAT_WS function.

To concatenate strings with a comma separator:

```sql
VALUES(CONCAT_WS(',', 'AB', 'CD', 'EF'));
```

This will return `'AB,CD,EF'`.

You can also concatenate more than two strings:

```sql
SELECT CONCAT_WS('/', 'ab', 'cd', 'ef') AS myconcat;
```

This will return `'ab/cd/ef'`.

The CONCAT_WS function will skip any NULL values:

```sql
SELECT CONCAT_WS('', 'ab', 'cd', NULL, 'ef') AS myconcat;
```

This will return `'abcdef'`.

The CONCAT_WS function can also take non-string arguments:

```sql
SELECT CONCAT_WS('', 123, 456, 3.14) AS myconcat;
```

This will return `'1234563.14'`. In this case, the integers and float values are implicitly converted to strings before concatenation.

However, the CONCAT_WS function expects at least two arguments. If fewer than two arguments are passed to the CONCAT_WS function, it will throw an error:

```sql
SELECT CONCAT_WS() AS myconcat;
```

This will throw an error because the CONCAT_WS function expects at least two arguments.
60 changes: 59 additions & 1 deletion docs/docs/sql-syntax/functions/text/concat.md
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
# CONCAT
# CONCAT

The CONCAT function in SQL concatenates two or more strings into one string.

## Syntax

The syntax for the CONCAT function in SQL is:

```sql
CONCAT ( string1, string2, ..., stringN )
```

## Parameters

- `string1`, `string2`, ..., `stringN`: These are the strings that you wish to concatenate together.

## Examples

Let's consider a few examples to understand how to use the CONCAT function.

To concatenate two strings:

```sql
SELECT CONCAT('ab', 'cd') AS myconcat;
```

This will return `'abcd'`.

You can also concatenate more than two strings:

```sql
SELECT CONCAT('ab', 'cd', 'ef') AS myconcat;
```

This will return `'abcdef'`.

If any string in the CONCAT function is NULL, the function will return NULL:

```sql
SELECT CONCAT('ab', 'cd', NULL, 'ef') AS myconcat;
```

This will return NULL.

The CONCAT function can also take non-string arguments:

```sql
SELECT CONCAT(123, 456, 3.14) AS myconcat;
```

This will return `'1234563.14'`. In this case, the integers and float values are implicitly converted to strings before concatenation.

However, the CONCAT function expects at least one argument. If no arguments are passed to the CONCAT function, it will throw an error:

```sql
SELECT CONCAT() AS myconcat;
```

This will throw an error because the CONCAT function expects at least one argument.
84 changes: 83 additions & 1 deletion docs/docs/sql-syntax/functions/text/find-idx.md
Original file line number Diff line number Diff line change
@@ -1 +1,83 @@
# FIND_IDX
# FIND_IDX

The `FIND_IDX` function in SQL is used to return the position of the first occurrence of a substring in a string, optionally after a specified position.

## Syntax

The syntax for the `FIND_IDX` function in SQL is:

```sql
FIND_IDX ( string, substring, [ start_position ] )
```

## Parameters

- `string`: The string where the search will take place.
- `substring`: The substring to find.
- `start_position` (optional): The position at which to start the search. The first position in the string is 0. If the `start_position` is not specified, the search starts from the beginning of the string.

## Examples

Let's consider a few examples to understand how to use the `FIND_IDX` function.

Find the position of 'rg' in 'pork':

```sql
SELECT FIND_IDX('pork', 'rg') AS test;
```

This will return 0, as 'rg' is not found in 'pork'.

Find the position of 'rg' in 'burger':

```sql
SELECT FIND_IDX('burger', 'rg') AS test;
```

This will return 3, as the first occurrence of 'rg' in 'burger' is at position 3.

Find the position of 'r' in 'pork', starting from position 4:

```sql
SELECT FIND_IDX('pork', 'r', 4) AS test;
```

This will return 0, as 'r' is not found in 'pork' after position 4.

Find the position of 'r' in 'burger', starting from position 4:

```sql
SELECT FIND_IDX('burger', 'r', 4) AS test;
```

This will return 6, as the first occurrence of 'r' in 'burger' after position 4 is at position 6.

Find the position of an empty string in 'cheese':

```sql
SELECT FIND_IDX('cheese', '') AS test;
```

This will return 0, because the search starts at the first position by default and the empty string is considered to be found at the start of any string.

Find the position of 's' in 'cheese':

```sql
SELECT FIND_IDX('cheese', 's') AS test;
```

This will return 5, as the first occurrence of 's' in 'cheese' is at position 5.

Find the position of 'e' in 'cheese burger', starting from position 5:

```sql
SELECT FIND_IDX('cheese burger', 'e', 5) AS test;
```

This will return 6, because the search starts from position 5 and the next 'e' is at position 6.

Using a NULL value as the substring will return NULL:

```sql
SELECT FIND_IDX('cheese', NULL) AS test;
```
Loading