Remove trailing whitespace from the input string.
RTRIM(str)
## Overview Removes whitespace characters from the end of the input string. Characters to the left of the final non-whitespace character are preserved unchanged, including any leading whitespace. RTRIM is the go-to tool for cleaning up fixed-width CHAR columns that pad their values with trailing spaces, CSV lines with CRLF or LF at the end, and any pipeline where trailing whitespace leaks in from a file reader or a network protocol. ## Behavior - Returns NULL when the input is NULL. - Returns an empty string when the input is empty or consists only of whitespace characters. - Whitespace is defined as the standard Unicode whitespace set: space (U+0020), tab (U+0009), line feed (U+000A), carriage return (U+000D), form feed (U+000C), vertical tab (U+000B), and the non-breaking space (U+00A0). - Leading whitespace and interior whitespace are preserved exactly. - Operates on UTF-8 code points, so multi-byte characters immediately before a run of trailing whitespace are kept intact. - Equivalent to TRIM(TRAILING FROM str) in SQL-standard syntax. ## Compatibility - RTRIM(str) is widely supported with identical semantics. - Some dialects also support RTRIM(str, chars) for a custom character set. DeltaForge accepts that extended form and treats it as BTRIM-style trimming restricted to the right side.
| Name | Type | Description |
|---|---|---|
str | Specifies the input string from which trailing whitespace is removed. |
-- Remove trailing spaces
SELECT RTRIM('hello '); -- 'hello'
-- Leading whitespace is preserved
SELECT RTRIM(' hello '); -- ' hello'
-- No trailing whitespace, result unchanged
SELECT RTRIM('hello'); -- 'hello'
-- Clean a column that was padded for fixed-width export
SELECT RTRIM(customer_code) AS customer_code
FROM ops.etl.staging_customers;
-- NULL propagates
SELECT RTRIM(CAST(NULL AS VARCHAR)); -- NULL
-- Whitespace-only input becomes empty
SELECT RTRIM(' '); -- ''