```
--------------------------------
### Select All from myspecies
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/examples/sql-eg-select/sql-eg-select-from-myspecies.md
Retrieves all columns and rows from the myspecies table to verify data insertion.
```sql
SELECT * from myspecies;
```
--------------------------------
### ASCII() Function Example: Get ASCII value from a column
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-ascii.md
Illustrates how to use the ASCII() function to retrieve the ASCII value of a string stored in a table column. This example includes table creation, data insertion, and a SELECT statement to display the results.
```sql
create table segments
(_id id, segment string);
insert into segments(_id, segment)
values (1,'r')
select _id, ascii(segment) as segment from segments;
+-----+----------+
| _id | segment |
+-----+----------+
| 1 | 114 |
+-----+----------+
```
--------------------------------
### Create VIEW for Customer Data Access Control (SQL)
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/statements/statement-view-create.md
This example demonstrates creating a 'customer' view that exposes only specific, shareable data from the 'person' table. It includes table creation, data insertion, view definition, and a query to show the view's contents.
```sql
create table person (_id id, dob timestamp, ssn string, name string);
insert into person(_id, dob, ssn, name)
values (1, '1950-01-01T00:00:00Z', '123456789', 'John Doe 1');
insert into person(_id, dob, ssn, name)
values (2, '1960-01-01T00:00:00Z', '123456780', 'John Doe 2');
insert into person(_id, dob, ssn, name)
values (3, '1970-01-01T00:00:00Z', '123456700', 'John Doe 3');
insert into person(_id, dob, ssn, name)
values (4, '2080-01-01T00:00:00Z', '123456000', 'John Doe 4');
create view customer as
select _id, name, datetimepart('yy',dob) birth_year
from person;
select * from customer order by birth_year;
```
--------------------------------
### CREATE TABLE cosvec-target SQL Example
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/_includes/sql-guide/sql-eg-table-create-statements.md
Example of creating a table named 'cosvec-target' with ID, STRING, and VECTOR data types. This is often related to insert operations into the same table.
```sql
CREATE TABLE cosvec-target (
id ID,
name STRING,
vector VECTOR
);
```
--------------------------------
### Build and Serve Documentation with Docker
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/local-build/local-build-with-WSL2.md
Steps to build the Docker image for the documentation and serve the Jekyll site using Docker Compose. Assumes Docker Desktop is running and you are in the project directory.
```shell
docker build - < Dockerfile
```
```shell
docker compose up serve
```
--------------------------------
### SQL: Get Day of Week from Date
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-datetimepart.md
Illustrates using the DATETIMEPART() function to determine the day of the week for a given timestamp, where Sunday is represented as 0. The example covers table setup and the query execution.
```sql
create table demo
(_id id, ts timestamp timeunit 's');
insert into demo(_id, ts)
values (1, '1970-01-01T00:00:00Z');
select _id, datetimepart('w',ts) from demo;
```
--------------------------------
### SQL: Create and Insert data into Colors table
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-offset.md
Demonstrates creating a 'colors' table and inserting sample data, which is used in subsequent OFFSET() query examples.
```sql
create table colors
(_id id, color string);
insert into colors(_id,color)
values (1,'green')
insert into colors(_id,color)
values (2,'red')
insert into colors(_id,color)
values (3,'yellow')
insert into colors(_id,color)
values (4,'blue')
insert into colors(_id,color)
values (5,'orange')
```
--------------------------------
### Create VIEW for Retiree Logic (SQL)
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/statements/statement-view-create.md
An example of creating a 'retiree' view that identifies individuals eligible for retirement based on their date of birth. It includes table creation, data insertion, view creation, and a query to display the results.
```sql
create table person (_id id, dob timestamp);
insert into person(_id, dob)
values (1, '1950-01-01T00:00:00Z');
insert into person(_id, dob)
values (2, '1960-01-01T00:00:00Z');
insert into person(_id, dob)
values (3, '1970-01-01T00:00:00Z');
insert into person(_id, dob)
values (4, '2080-01-01T00:00:00Z');
create view retiree as
select _id, datetimeadd('yy',62,dob) retirement_dt
from person
where datetimeadd('yy',62,dob)>current_timestamp;
select * from retiree order by retirement_dt;
```
--------------------------------
### Use LEN() on a column - SQL
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-len.md
Demonstrates how to use the LEN() function to get the length of a string stored in a column. This example creates a table, inserts data, and then selects the ID and the length of the 'segment' column.
```sql
create table segments
(_id id, segment string);
insert into segments(_id, segment)
values (1,'testing')
select _id, len(segment) as length from segments;
+-----+----------+
| _id | length |
+-----+----------+
| 1 | 7 |
+-----+----------+
```
--------------------------------
### CREATE TABLE parquet-target SQL Example
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/_includes/sql-guide/sql-eg-table-create-statements.md
Example of creating a 'parquet-target' table using ID, INT, and DECIMAL data types. This table structure is often used with bulk insert operations.
```sql
CREATE TABLE parquet-target (
item_id ID,
quantity INT,
price DECIMAL(10, 2)
);
```
--------------------------------
### fbsql Run Query and Verify Output File Content
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/tools/fbsql/fbsql-config-output.md
This example executes a `select` query and then uses `\! cat` to display the content of the output file, showing both the comment and the query results.
```bash
select * from products;
\! cat output-test.sql
Testing 1,2,3
_id | item | price | stock
-----+----------------------+--------+-------
1 | pen | 2.50 | NULL
2 | pencil | 0.50 | NULL
3 | playpen | 52.50 | NULL
4 | gold-plated earplugs | 122.50 | NULL
(0 rows)
```
--------------------------------
### DATETIMENAME() - Get Day of Week Name
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-datetimename.md
Illustrates the usage of the DATETIMENAME() function to obtain the name of the day of the week for a specific date. This example includes table creation, data insertion, and a query to extract the weekday name.
```sql
create table demo
(_id id, ts timestamp timeunit 's');
insert into demo(_id, ts)
values (1, '1970-01-01T00:00:00Z');
select _id, datetimename('w',ts) from demo;
```
--------------------------------
### Handle NULL input parameters with DATETIMEDIFF()
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-datetimediff.md
Demonstrates how the DATETIMEDIFF() function handles `null` input parameters. If any argument is `null`, the function returns `NULL`. This example shows table setup, data insertion, and a query with a `null` timeunit.
```sql
create table demo
(_id id, start-date timestamp timeunit 's', end-date timestamp timeunit 's');
insert into demo(_id, start-date, end-date)
values (1, '2023-02-27T21:30:00Z', '2023-03-01T21:30:00Z');
select _id, datetimediff(null, start-date, end-date) as diff from demo;
_id | diff
-----+------
```
--------------------------------
### Real-World Syntax Application Example
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/style-guide/template-reference.md
Shows how to present a real-world application of the documented syntax. This section should include a brief explanation of the code block's purpose and its practical use.
```markdown
# real world application of syntax
```
--------------------------------
### Install ebnf2railroad via npm
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/regenerate-sql-svg/README.md
Installs the ebnf2railroad package globally using npm, which is required for generating BNF diagrams.
```shell
sudo npm install -g ebnf2railroad
```
--------------------------------
### Calculate days difference between two dates using DATETIMEDIFF()
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-datetimediff.md
Calculates the difference in days between two specific dates. This example demonstrates table creation, data insertion, and querying the date difference. It handles cases where the end date is after the start date.
```sql
create table demo
(_id id, start-date timestamp timeunit 's', end-date timestamp timeunit 's');
insert into demo(_id, start-date, end-date)
values (1, '2023-02-27T21:30:00Z', '2023-03-01T21:30:00Z');
select _id, datetimediff('d', start-date, end-date) as days_diff from demo;
_id | days_diff
-----+-----------
```
--------------------------------
### Internal Hyperlink Example
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/writing-help/writing-hyperlinks.md
Demonstrates the structure for creating an internal hyperlink to a specific document within the FeatureBase documentation.
```markdown
`[meaningful-anchor-text](/docs/folder/filename)`
`[Learn how to create tables in FeatureBase cloud](/docs/cloud/cloud-tables/cloud-table-create)`
```
--------------------------------
### Success Callout Implementation
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/writing-help/writing-content-callouts.md
Provides examples of creating 'success' callouts using HTML and Markdown. This callout type is for infrequent use but available when needed.
```html
Q'PLA! This is a success callout with a joke because it's that time of the day.
```
```markdown
{: .success }
Q'PLA! This is a success callout with a joke because it's that time of the day.
```
--------------------------------
### COALESCE() SQL Function Examples
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-coalesce.md
Demonstrates the usage of the COALESCE() function in FeatureBase SQL to replace NULL values with specified defaults. Includes examples for handling single null columns, multiple null columns, and numeric null values.
```SQL
CREATE TABLE stock
(__id ID, product STRING, brand STRING, subcategory STRING, category STRING, family STRING, quantity_available INT, minimum_to_have INT);
INSERT INTO stock
VALUES
(1, 'pork ribs', NULL, 'pork meat', 'meat','food',400, 130),
(2, 'tomatoes','Mr Red', NULL, 'vegetables','food',280, 100),
(3,'lettuce',NULL, 'Leaf vegetables', NULL,'food',280, Null),
(4,'Bananas',NULL, NULL,NULL,'food',Null, NULL),
(5,'hamburger','MaxBurg','cow meat','meat','food',220, 150),
(6,'hamburger','SuperBurga',Null,Null,Null,125, Null);
SELECT * FROM stock;
SELECT product, COALESCE(brand, 'locally grown') AS final_brand FROM stock;
SELECT product, COALESCE(subcategory, category, family, 'no product description') AS product_and_subcategory FROM stock;
SELECT _id, COALESCE(quantity_available, minimum_to_have, 100) AS quantity FROM stock;
```
--------------------------------
### Create a Sample Cloud Database
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/cloud/cloud-databases/cloud-db-manage.md
Learn how to create a sample database in FeatureBase Cloud. This is a summary of the process.
```APIDOC
## Learn how to create a sample database
### Description
This section provides instructions on creating a sample database within the FeatureBase Cloud environment.
### Method
GET
### Endpoint
/docs/cloud/cloud-databases/cloud-db-create-sample
```
--------------------------------
### DATE_TRUNC() SQL Example: Display Date, Time with Hours and Minutes
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-date_trunc.md
Shows how to use DATE_TRUNC() to display the date and time, including hours and minutes, but excluding seconds. The example creates a table, inserts data, and uses DATE_TRUNC('mi', ts).
```sql
create table demo
(_id id, ts timestamp timeunit 's');
insert into demo(_id, ts)
values (1, '1970-01-01T00:00:00Z');
select _id, date_trunc('mi',ts) from demo;
```
--------------------------------
### ASCII() Function Syntax
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-ascii.md
Demonstrates the basic syntax for using the ASCII() function in FeatureBase SQL.
```sql
ascii(expr)
```
--------------------------------
### Calculate hours difference when end time is before start time using DATETIMEDIFF()
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-datetimediff.md
Calculates the difference in hours between two datetime values, demonstrating a scenario where the end time precedes the start time, resulting in a negative difference. It includes table creation and data insertion.
```sql
create table demo
(_id id, start-date timestamp timeunit 's', end-date timestamp timeunit 's');
insert into demo(_id, start-date, end-date)
values (1, '2023-03-01T12:00:00Z', '2023-03-01T09:00:00Z');
select _id, datetimediff('h', start-date, end-date) as hours_diff from demo;
_id | hours_diff
-----+------------
```
--------------------------------
### SQL: Extract Substring from Column
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-substring.md
Extracts a substring from the 'segment' column of the 'segments' table, starting at index 0 with a length of 3 characters.
```SQL
create table segments
(_id id, segment string);
insert into segments(_id,segment)
values (1,'green')
select _id, substring(segment,0,3) as substr from segments;
+-----+----------+
| _id | substr |
+-----+----------+
| 1 | gre |
+-----+----------+
```
--------------------------------
### Page Title Example - Markdown
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/style-guide/custom-heading-page-title.md
This snippet demonstrates the standard format for a page title using Markdown. It's typically the first heading on a documentation page.
```markdown
# How do I write FeatureBase Docs?
```
--------------------------------
### SQL: Extract Substring from Reversed String
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-substring.md
Extracts a substring from a reversed string in the 'segment' column, starting at index 1. The REVERSE() function is used first.
```SQL
create table segments
(_id id, segment string);
insert into segments(_id,segment)
values (1,'red')
select _id, substring(reverse(segment), 1) as substr from segments;
+-----+----------+
| _id | substr |
+-----+----------+
| 1 | er |
+-----+----------+
```
--------------------------------
### Rewritten Single Word Hyperlink Example
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/writing-help/writing-hyperlinks.md
Illustrates how to improve content by replacing single-word hyperlinks with more descriptive text and structuring the information better.
```markdown
Original:
>Everything that can be done in the user interface can be accomplished via REST api calls. Furthermore, APIs allow you to perform additional actions as well as gather more metadata about your organization and data. You will likely interact with the APIs in a production setting. Full Documentation for the APIs can be found [here](/cloud/cloud-api).
Rewritten as:
>REST API calls will tend to be used in production environments:
> * instead of the FeatureBase user interface
> * to gather more metadata about your organization and data
>
> ## Further information
>
> * [Full API documentation](/cloud/cloud-api)
```
--------------------------------
### Show Columns Syntax
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/statements/statement-columns-show.md
The basic syntax for the SHOW COLUMNS statement, used to display the schema of a specified table.
```sql
SHOW COLUMNS FROM table_name;
```
--------------------------------
### FeatureBase TopK Query Example
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/cloud/cloud-query/cloud-query-sample-db.md
A simple FeatureBase TopK query to find the top 5 most frequent 'hobbies' in the 'cseg' table.
```featurebase
[cseg]TopK(hobbies, k=5)
```
--------------------------------
### Testing Set Membership as a Where Clause Filter
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-set.md
Example of using a SET function (SETEXCLUDES) in the WHERE clause to filter rows where a specific value is not present in the set column.
```sql
SELECT _id FROM segments WHERE
SETEXCLUDES(segment, 'purple');
```
--------------------------------
### Create Impala Source Table
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/tools/fbsql-examples/fbsql-loader-eg-impala-source.md
SQL statement to create a sample table in an Impala database. This table structure is used as the source for data import into FeatureBase.
```sql
CREATE TABLE testdb.impala_table (
idkey int,
intf int,
stringf string,
idf int,
stringsetf string,
idsetf string);
```
--------------------------------
### SQL: Extract suffix from string column
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-suffix.md
This example demonstrates how to use the SUFFIX() function to extract a specified number of characters from the end of a string stored in a database column.
```sql
create table segments
(_id id, segment string);
insert into segments(_id,segment)
values (1,'FOOBAR')
select _id, suffix(segment, 3) as convertedstr from segments;
+-----+---------------+
| _id | convertedstr |
+-----+---------------+
| 1 | BAR |
+-----+---------------+
```
--------------------------------
### Serve Jekyll Site Locally
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/local-build/local-build-with-vm.md
This command serves the Jekyll site locally, allowing developers to preview changes in real-time. A restart is required for changes in `_config.yml`.
```shell
bundle exec jekyll serve
```
--------------------------------
### FeatureBase SQL DATETIMEADD() - Calculate Past Date
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-datetimeadd.md
This example shows how to use a negative time duration with DATETIMEADD() to calculate a date 100 days prior to the current date.
```sql
select datetimeadd('d', -100, current_date) as hundred_days_ago_today;
```
--------------------------------
### BULK INSERT ORC Data (SQL)
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/_includes/sql-guide/sql-eg-insert-bulk-statements.md
Inserts data from an ORC file into a FeatureBase table. Configuration options include format and input source.
```sql
BULK INSERT orc-target
FROM 'URL_TO_ORC'
WITH (
FORMAT = 'ORC',
INPUT = 'URL'
);
```
--------------------------------
### Connect to FeatureBase using psql CLI
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/writing-help/writing-procedures.md
This snippet shows how to connect to a FeatureBase instance using the psql command-line interface. It assumes FeatureBase is running on localhost with a specific port.
```shell
psql -h localhost -p 55432
```
--------------------------------
### INSERT statement with STRINGSET data types
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/statements/statement-insert.md
This example demonstrates inserting multiple string values into a STRINGSET column in the 'myspecies' table. It shows how to provide a list of strings for a single '_id'.
```sql
INSERT
into myspecies (_id, species)
values
('yes', ['Manatee', 'Sea Horse', 'Koala']),
('no', ['Starfish']);
```
--------------------------------
### INSERT statement overwriting existing values
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/statements/statement-insert.md
This example illustrates how the INSERT statement overwrites existing data when a record with a duplicate '_id' is encountered in the 'services' table. This is useful for updating records.
```sql
INSERT INTO services (_id, servicelist, price)
VALUES (2, 'local postage per item', 2.20);
```
--------------------------------
### SQL EXPLAIN Example: CREATE TABLE
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/statements/statement-explain.md
Demonstrates how to use the EXPLAIN statement with a CREATE TABLE query. It shows the SQL command and the resulting JSON output which represents the execution plan for creating the 'doctest' table.
```sql
EXPLAIN CREATE TABLE doctest (_id ID, stringcol STRING);
```
```json
{
"_op": "*planner.PlanOpQuery",
"_schema": [],
"child": {
"_op": "*planner.PlanOpCreateTable",
"ifNotExists": false,
"tableName": "doctest"
},
"sql": "EXPLAIN CREATE TABLE doctest (_id ID, stringcol STRING);",
"warnings": []
}
```
--------------------------------
### Basic INSERT statement
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/statements/statement-insert.md
This snippet shows the fundamental syntax for the INSERT statement, specifying the target table, optional column list, and the corresponding value list.
```sql
INSERT INTO
[()]
VALUES
{(),...};
```
--------------------------------
### SET and SETQ Syntax
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/data-types/data-type-set-setq.md
Defines the general syntax for the SET and SETQ data types, including optional TIMEQUANTUM and TTL constraints for SETQ.
```sql
{ID | STRING}SET{Q TIMEQUANTUM '' [TTL '']}
```
--------------------------------
### Insert Data into Impala Source Table
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/tools/fbsql-examples/fbsql-loader-eg-impala-source.md
SQL statement to insert sample data into the Impala source table. This data will be imported into FeatureBase.
```sql
INSERT INTO testdb.impala_table VALUES
(0, 0, 'a', 0, 'a', '3'),
(1, 0, 'a', 0, 'c', '4'),
(2, 0, 'a', 0, 'd', '5');
```
--------------------------------
### Testing Set Membership with Multiple Conditions
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-set.md
Example demonstrating the use of multiple SETEXCLUDES functions combined with an AND operator in the WHERE clause to filter rows based on multiple set membership criteria.
```sql
SELECT _id FROM segments WHERE
SETEXCLUDES(segment, 'purple') AND
SETEXCLUDES(segment, 'yellow');
```
--------------------------------
### Testing Set Membership in Select List
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-set.md
Example of using a SET function (SETEXCLUDES) within the SELECT list to test if a value is not present in a set column. Returns a boolean result.
```sql
SELECT SETEXCLUDES(segment, 'purple')
AS NOTPURPLE
FROM segments;
```
--------------------------------
### Download FeatureBase Python Client via Git
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/tools/python-client-library/python-client-install.md
Clones the FeatureBase Python client library repository from GitHub using Git. This method is useful for developers who want to access the source code directly.
```bash
git clone git@github.com:FeatureBaseDB/python-featurebase.git
```
--------------------------------
### DATETIMENAME() - Get Month Name
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-datetimename.md
Demonstrates how to use the DATETIMENAME() function to retrieve the name of the month from a given timestamp. It involves creating a table, inserting a sample timestamp, and then querying the month name.
```sql
create table demo
(_id id, ts timestamp timeunit 's');
insert into demo(_id, ts)
values (1, '1970-01-01T00:00:00Z');
select _id, datetimename('m',ts) from demo;
```
--------------------------------
### CHARINDEX() SQL Example
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-charindex.md
Demonstrates the basic usage of the CHARINDEX() function to find the starting position of a substring within a string. It includes table creation, data insertion, and a SELECT statement to retrieve the position.
```sql
create table customers
(_id id, segment string);
insert into customers(_id,segment)
values (1,'this is great')
select _id, charindex('is',segment) as charindex from customers;
```
--------------------------------
### Code Blocks with Language Specification (Go)
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/writing-help/writing-rules.md
Shows how to use triple backticks to create code blocks, specifying the programming language (Go) for syntax highlighting.
```go
```go
```
--------------------------------
### fbsql CLI Syntax for Output Flags
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/tools/fbsql/fbsql-config-output.md
Displays the general syntax for fbsql output-related commands, including meta-prefix options and commands like cd, out, qecho, and warn.
```bash
()
[--history-path=""] |
[cd []] |
[[o | out] ] |
[qecho ] |
[warn ]
]
```
--------------------------------
### DATE_TRUNC() SQL Example: Display Year and Month
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-date_trunc.md
Demonstrates how to use the DATE_TRUNC() function to display only the year and month from a timestamp. It involves creating a table, inserting data, and querying with DATE_TRUNC('m', ts).
```sql
create table demo
(_id id, ts timestamp timeunit 's');
insert into demo(_id, ts)
values (1, '1970-01-01T00:00:00Z');
select _id, date_trunc('m',ts) from demo;
```
--------------------------------
### fbsql CLI Syntax Overview
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/tools/fbsql/fbsql-running-sql.md
This snippet outlines the general syntax for interacting with the fbsql command-line interface, including meta-flags for connection, schema inspection, variable management, and query execution.
```bash
[
()
[ c|connect [ | - ] ] |
[ d[< tablename>|t|v ] ] |
[ l|list ] |
[ set [variable-value,...] ] |
[ unset ] |
[ i|include ] |
[ watch ] |
[ t|timing [on|off] ] |
[ p|print ] |
[ r|reset ] |
]
```
--------------------------------
### Serve Docker Container
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/help-on-help/local-build/local-build-with-docker.md
Serves the Docker container to view the local site. Changes to configuration files like `/_config.yml` are not loaded automatically while the site is running. Run this command in the `/featurebase-docs` directory.
```bash
docker compose up serve
```
--------------------------------
### Use TOTIMESTAMP() in SELECT Query with Filtering (SQL)
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-totimestamp.md
This example demonstrates using the TOTIMESTAMP() function within a SELECT query to convert an integer column to a timestamp and then filtering the results based on this converted timestamp.
```sql
create table demo
(_id id, int_ts int);
insert into demo(_id, int_ts)
values (1, 86400);
insert into demo(_id, int_ts)
values (2, 86400);
insert into demo(_id, int_ts)
values (3, 86400000);
select _id, int_ts, TOTIMESTAMP(int_ts, 's') as ts
from demo;
select _id, int_ts, TOTIMESTAMP(int_ts, 's') as ts
from demo
where TOTIMESTAMP(int_ts, 's')>'1970-01-02T00:00:00Z';
```
--------------------------------
### Convert Granular Integers to Timestamp with TOTIMESTAMP() (SQL)
Source: https://github.com/featurebasedb/featurebase-docs/blob/main/docs/sql-guide/functions/function-totimestamp.md
This example shows how to use the TOTIMESTAMP() function with different time units ('s', 'ms', 'us', 'ns') to convert integer values with varying granularities into timestamps.
```sql
create table demo
(_id id, ts timestamp timeunit 's');
insert into demo(_id, ts)
values (1, TOTIMESTAMP(90061));
insert into demo(_id, ts)
values (2, TOTIMESTAMP(90061, 's'));
insert into demo(_id, ts)
values (3, TOTIMESTAMP(90061000,'ms'));
insert into demo(_id, ts)
values (4, TOTIMESTAMP(90061000000,'us'));
insert into demo(_id, ts)
values (5, TOTIMESTAMP(90061000000000,'ns'));
select _id, ts from demo;
```