SQL SUBSTRB function
Oracle's byte-oriented SUBSTR: positions and lengths count bytes, not characters.
- Categoria
- Strings
- Returns
string- In this category
- 100 of 172
Sintaxe
SUBSTRB(str, start_byte)
SUBSTRB(str, start_byte, length_bytes)
Each block is a separate form. The support table and the examples show which engine takes which.
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | No SUBSTRB; SUBSTRING on a varbinary cast counts bytes. |
| PostgreSQL | Not built in | No SUBSTRB; substring() on convert_to(s, 'UTF8') counts bytes. |
| MySQL | Not built in | No SUBSTRB; SUBSTRING on a BINARY cast counts bytes. |
| SQLite | Not built in | No SUBSTRB; SUBSTR on a BLOB counts bytes. |
| Oracle | Sim | Supported under this name. |
Exemplos
Oracle
SELECT SUBSTRB(hex_hash, 1, 16) AS first_16_bytes FROM digests;
Oracle
SELECT LENGTHB(note) AS bytes, LENGTH(note) AS chars FROM messages;
Bom saber
- In UTF-8 one character takes one to four bytes. When the byte range starts or ends inside a character, Oracle replaces the partial character with a space: SUBSTRB('aéb', 1, 2) in an AL32UTF8 database is 'a ' rather than 'aé'.
- Use SUBSTRB when the limit really is in bytes: fixed single-byte data, hex strings, or fields a downstream system caps by byte length. Use SUBSTR for user-visible text.
- LENGTHB is the byte-counting companion of LENGTH; comparing the two shows which rows hold multi-byte characters.
Questions about SUBSTRB
When should I use SUBSTRB instead of SUBSTR?
When the value is defined in bytes: fixed-width single-byte encodings, or interfaces that cap a field at N bytes rather than N characters.
Why does SUBSTRB return trailing blanks?
The cut landed inside a multi-byte character, and Oracle pads the partial character with spaces. Use SUBSTR, or cut by LENGTHB-aware logic, for text.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.