Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Oracle SQL Strings: A Practical How-To Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Oracle SQL treats strings as values of the VARCHAR2, NVARCHAR2, CHAR, NCHAR, CLOB, or NCLOB data types, and you work with them using literal syntax (single quotes), concatenation (the || operator), and built-in functions such as LENGTH, SUBSTR, INSTR, UPPER, LOWER, TRIM, REPLACE, and LPAD/RPAD. To get started you only need access to an Oracle Database instance and a SQL client (SQL*Plus, SQL Developer, or any tool that connects via JDBC/ODBC). The steps below show how to create string values, inspect them, search and modify them, and avoid the mistakes that cause ORA errors or wrong results.

What You'll Need

  • An Oracle Database account with permission to run SELECT queries (and CREATE TABLE/INSERT if you want to store test strings).
  • A SQL client such as SQL*Plus, Oracle SQL Developer, or another tool that connects to Oracle.
  • Basic familiarity with SQL SELECT statements.

Step-by-Step

  1. Write a string literal correctly. In Oracle SQL, string literals are enclosed in single quotes: 'Hello'. To include a single quote inside the string, double it: 'It''s fine'. Double quotes are for identifiers (column or table names), not string values, so "Hello" will be treated as an identifier and usually raise ORA-00904.
  2. Concatenate strings with the double pipe operator. SELECT 'Hello' || ' ' || 'World' FROM dual; returns Hello World. The || operator is the standard Oracle concatenation operator; the CONCAT function exists but accepts only two arguments, so use || when joining more than two values.
  3. Check the length of a string. SELECT LENGTH('Oracle') FROM dual; returns 6. LENGTH counts characters, not bytes, which matters for multibyte character sets. Use LENGTHB if you specifically need byte length. For CLOB columns, LENGTH works but LENGTHB may behave differently depending on the database version and character set, so test on your own data.
  4. Extract part of a string with SUBSTR. SELECT SUBSTR('Oracle SQL', 1, 6) FROM dual; returns Oracle. The second argument is the starting position (1-based, not 0-based), and the third is the number of characters. Omit the third argument to return everything from the start position to the end. A negative start position counts from the end of the string.
  5. Find the position of a substring with INSTR. SELECT INSTR('Oracle SQL', 'SQL') FROM dual; returns 8. INSTR returns 0 when the substring is not found, which is useful in WHERE clauses: WHERE INSTR(column_name, 'search') > 0. You can also supply a start position and an occurrence number as additional arguments.
  6. Change case with UPPER, LOWER, and INITCAP. SELECT UPPER('Oracle'), LOWER('Oracle'), INITCAP('oracle sql') FROM dual; returns ORACLE, oracle, and Oracle Sql. These are commonly used to make comparisons case-insensitive: WHERE UPPER(column_name) = UPPER(:input).
  7. Remove unwanted characters with TRIM, LTRIM, and RTRIM. SELECT TRIM(' hello ') FROM dual; returns hello. TRIM removes spaces (or a specified character) from both ends; LTRIM and RTRIM remove from the left or right only. Note that TRIM only removes a single character, not a set of characters, so for more complex cleanup use REGEXP_REPLACE.
  8. Replace text with REPLACE. SELECT REPLACE('Oracle SQL', 'SQL', 'Database') FROM dual; returns Oracle Database. REPLACE replaces all occurrences by default; there is no built-in option to replace only the first occurrence, so use REGEXP_REPLACE with a count argument if you need that.
  9. Pad strings with LPAD and RPAD. SELECT LPAD('7', 3, '0') FROM dual; returns 007. This is handy for formatting fixed-width codes or aligning output. RPAD pads on the right. If the target length is shorter than the input, both functions truncate the string.
  10. Use pattern matching with LIKE and REGEXP_LIKE. WHERE column_name LIKE 'A%' matches values starting with A. The underscore _ matches exactly one character, and % matches any number of characters. To search for a literal percent sign, use the ESCAPE clause: LIKE '100\%' ESCAPE '\'. For regular expressions, use REGEXP_LIKE, REGEXP_SUBSTR, and REGEXP_REPLACE.
  11. Handle NULLs explicitly. In Oracle, an empty string '' is treated as NULL, which surprises many people coming from other databases. Concatenating NULL with || yields the other operand (Oracle treats NULL as an empty string in concatenation), but comparisons with NULL using = return unknown, so use IS NULL or IS NOT NULL. Functions like LENGTH(NULL) return NULL, not 0.
  12. Store strings in the right data type. Use VARCHAR2 for variable-length character data (maximum 4000 bytes in SQL, 32767 in PL/SQL), CHAR for fixed-length data, and CLOB for large text. NVARCHAR2 and NCLOB store Unicode data. Avoid the older VARCHAR type; Oracle recommends VARCHAR2 because its behavior is guaranteed to remain consistent.

Common Mistakes

  • Using double quotes for string literals. Double quotes denote identifiers, so "text" fails unless a column or table with that exact name exists.
  • Forgetting to double a single quote inside a literal. 'It's' causes a syntax error; write 'It''s'.
  • Assuming an empty string is different from NULL. In Oracle they are the same, so WHERE col = '' never matches anything.
  • Using 0-based positions in SUBSTR or INSTR. Oracle string functions are 1-based, so SUBSTR('abc', 0, 1) returns 'a' because position 0 is treated as 1.
  • Comparing CHAR columns without accounting for trailing spaces. CHAR pads values to the declared length, so 'A' stored in CHAR(5) becomes 'A ', which can break equality checks.
  • Relying on implicit conversion between numbers and strings. Oracle will convert in many cases, but the results can depend on NLS settings; use TO_CHAR and TO_NUMBER explicitly.

Tips

  • Use quoting for strings that contain many single quotes: q'[It's a test]' avoids doubling every apostrophe.
  • Prefer bind variables over concatenating user input into SQL strings to avoid SQL injection and to improve cursor reuse.
  • Use DUMP() to inspect the exact bytes and data type of a string when debugging unexpected comparisons.
  • Remember that NLS settings affect sorting and case conversion for some characters; test with your database's actual character set.
  • For large text, use DBMS_LOB package procedures rather than string functions, which may be limited on CLOB values.

Troubleshooting

If you get ORA-00904 (invalid identifier), check that you used single quotes for string values and double quotes only for identifiers. If ORA-01756 (quoted string not properly terminated) appears, look for an unescaped apostrophe inside a literal. If a comparison returns no rows when you expect matches, check for trailing spaces (use TRIM), case differences (use UPPER or LOWER), or NULL values (use IS NULL). If SUBSTR returns unexpected results, confirm the start position and length, remembering that positions are 1-based and that a negative start counts from the end. If LENGTH returns a number larger than expected, the column may be CHAR rather than VARCHAR2, so trailing spaces are being counted; use TRIM before LENGTH or change the column type. If REGEXP functions behave unexpectedly, test the pattern with a simple SELECT before using it in a WHERE clause.

Frequently Asked Questions

How do I include a single quote in an Oracle string?

Double it: 'It''s'. Alternatively, use the alternative quoting mechanism q'[It's]'.

What is the difference between VARCHAR and VARCHAR2?

Oracle recommends VARCHAR2 because its behavior is guaranteed to remain consistent across versions. VARCHAR is currently synonymous but may change in the future, so use VARCHAR2 for new work.

Why does my empty string comparison fail?

Oracle treats an empty string as NULL, so col = '' is never true. Use col IS NULL instead.

How do I concatenate more than two strings?

Use the || operator repeatedly, for example 'a' || 'b' || 'c'. The CONCAT function only accepts two arguments.

How can I make a string comparison case-insensitive?

Apply UPPER or LOWER to both sides, or use the NLS_COMP and NLS_SORT session settings if your application requires it.

What is the maximum length of a VARCHAR2 in Oracle SQL?

In SQL, VARCHAR2 is limited to 4000 bytes by default (32767 bytes if MAX_STRING_SIZE is set to EXTENDED). In PL/SQL, the limit is 32767 bytes. For larger text, use CLOB.