Convert the input string to snake_case.
TO_SNAKE_CASE(str)
## Overview Converts the input string to snake_case by lowercasing the entire string and inserting underscores at word boundaries. Word boundaries include the transition from lowercase to uppercase, as well as spaces and hyphens. Use this function to turn JSON or display names into database-friendly column names. ## Behavior - Returns NULL if the input is NULL. - Empty input returns an empty string. - Uppercase transitions insert an underscore before the uppercase letter. - Spaces and hyphens are replaced with a single underscore. - Consecutive separators collapse into a single underscore. - Leading and trailing underscores are trimmed. - Deterministic and side effect free. ## Compatibility - Matches the PG-compat TO_SNAKE_CASE semantics.
| Name | Type | Description |
|---|---|---|
str | Specifies the input string. Uppercase boundaries, spaces, and hyphens are converted into underscores. |
-- From camelCase
SELECT TO_SNAKE_CASE('firstName'); -- 'first_name'
-- From spaces
SELECT TO_SNAKE_CASE('Hello World'); -- 'hello_world'
-- From PascalCase
SELECT TO_SNAKE_CASE('BackgroundColor'); -- 'background_color'
-- Normalize JSON keys into SQL column names
SELECT TO_SNAKE_CASE(json_key) AS db_column
FROM stage.raw.schema_probe;
-- NULL propagation
SELECT TO_SNAKE_CASE(NULL); -- NULL