Databricks STRING Data Type: Uses and Examples

How-to basics · Updated November 22, 2026

In Databricks, STRING is the data type for text: it holds sequences of characters such as names, descriptions, identifiers, and JSON fragments. It is the standard choice when a column contains text and you do not need a numeric, date, or binary type.

What STRING means in Databricks

STRING is a variable-length character type in Databricks SQL and Apache Spark. A value can be a short label, a sentence, or a much longer text field; the type declaration itself does not require you to specify a character limit. This does not mean a value can be infinitely large: practical limits still apply to the engine, a row, and the operation being performed.

Databricks uses the same logical type across its SQL and Spark data-processing interfaces. In Spark’s type system, the corresponding type is StringType. A SQL column declared as STRING can therefore be read and manipulated through a Spark DataFrame as a string column, rather than requiring a special conversion just to change interfaces.

Typical values include customer names, product codes that may contain letters, free-form comments, file paths, and source-system fields whose format is not yet known. A value that looks like a number is still often best represented as a string when it is an identifier: postal codes, account numbers, and codes with leading zeroes should not normally undergo arithmetic or have those zeroes removed.

Declaring STRING columns in SQL

Use STRING in a table definition to declare a text column. For example:

CREATE TABLE customers (
  customer_id BIGINT,
  customer_name STRING,
  notes STRING
);

You can also use it in a query to convert an expression to text:

SELECT CAST(order_id AS STRING) AS order_id_text
FROM orders;

The cast changes the expression’s type; it does not turn text back into a number automatically later. Choose an explicit conversion when you need arithmetic, date handling, or a comparison that depends on numeric ordering. For instance, the text values '10' and '2' may sort alphabetically as strings, while the corresponding numbers sort by numeric value.

A string literal in Databricks SQL is enclosed in single quotation marks, as in 'North'. To include a single quote within a literal, double it: 'O''Brien'. This rule applies to SQL literals, not to values passed separately as parameters by an application or notebook.

Using STRING with PySpark and DataFrames

In PySpark, Spark represents string columns with StringType. If a DataFrame is created from Python records, Spark can infer the type from string values. For a more controlled schema, declare it explicitly:

from pyspark.sql.types import StringType, StructField, StructType

schema = StructType([
    StructField('customer_name', StringType(), True)
])

The final argument to StructField indicates whether the field is nullable. A nullable string column can contain a missing value represented in Python as None; that is different from an empty string such as ''. An empty string is a present value with zero characters, while null means that the value is absent or unknown.

For DataFrame transformations, functions such as cast, concat, and trim let you convert or modify text. For example, df.select(df.order_id.cast('string')) produces a string expression for an order ID. In Spark SQL, use the corresponding SQL expression or function. The DataFrame column’s logical type is useful to check when a source file’s inferred schema is inconsistent or a transformation has changed a field unexpectedly.

STRING compared with VARCHAR and CHAR

STRING is the general-purpose option for text without a declared maximum length. Databricks SQL also supports VARCHAR(n), which describes a variable-length string with a specified character limit. Use that when a limit is part of the data contract and you want the schema to communicate it. A value that violates the limit can be rejected, so confirm the behavior and compatibility of the target table and runtime before adopting it.

CHAR(n) is a fixed-length character type. Fixed-length semantics can involve padding, which may surprise users who expect it to behave exactly like an ordinary variable-length field. Unless a fixed-width format is required, STRING is usually simpler for names and general text.

  • Choose STRING for unrestricted text fields and general-purpose text processing.
  • Choose VARCHAR(n) when a maximum character count is a meaningful schema rule.
  • Consider CHAR(n) only when fixed-width character semantics are actually needed.
  • Use a numeric or date type instead when values need numeric calculations or date operations.

These types are not substitutes for validation in every application. A database column type can express some constraints, but it does not by itself ensure that a name follows a business rule, that an identifier has the correct pattern, or that a description is useful.

Character length, Unicode, and comparison

Text is not limited to basic English letters. Databricks strings can contain Unicode characters, including accented letters, characters from non-Latin writing systems, and emoji. A displayed symbol may consist of multiple code points, so a length function’s count does not always match what a person sees as one visual character. Combining marks and joined emoji are common reasons for the difference.

Databricks SQL provides string functions such as length and char_length to measure text. octet_length measures the number of bytes instead, which matters when checking encoded data sizes. These measurements answer different questions: a text length is not a reliable substitute for a byte limit, and a byte count does not tell you how many visual symbols a user sees.

String comparisons and sorting can also depend on collation. With the ordinary default behavior, comparisons are generally case-sensitive and based on the string values’ binary ordering; that may not match the expectations of a human-language sort. Databricks supports collations in applicable environments, allowing other comparison rules to be specified. Check the Databricks SQL or Runtime version and the chosen collation before relying on case-insensitive comparisons or language-specific ordering.

How STRING behaves in tables and files

When a string column is written to a Delta table, its type is part of the table’s logical schema. Delta Lake uses Parquet for the underlying data files, but readers should work with the table schema rather than infer meaning from the physical file representation. A column that is logically a string remains text even when its contents happen to look numeric.

Source files can introduce ambiguity. CSV, for example, is plain text and may not carry reliable type information. During ingestion, decide whether each field should be treated as a string or converted to another type, and handle headers, delimiters, quotes, and null markers consistently. Schema inference is convenient for exploration, but an explicit schema is safer for repeatable production pipelines because the same input is less likely to be interpreted differently after values change.

For semi-structured content, storing the entire document as a string may be useful for retaining the original text. If you need to filter or transform individual fields, however, parse it into a structured representation such as a STRUCT, ARRAY, or MAP, or use the appropriate JSON functions. Keeping JSON text does not make its internal fields typed or directly available as ordinary table columns.

Common STRING problems and how to avoid them

  • Unexpected alphabetical sorting: A string column sorts as text, not as a number. Convert values to a numeric type for numeric ordering, after checking that every value can be converted safely.
  • Lost leading zeroes: If an identifier such as 00127 is inferred as an integer, its leading zeroes disappear. Preserve identifiers as strings at ingestion when those zeroes are significant.
  • Null versus blank: A null and an empty string are different values. Decide how the source represents missing data, and normalize only when that is appropriate for the application.
  • Whitespace mismatches: A value can contain leading or trailing spaces that make equality checks or grouping results surprising. Use trimming deliberately, and do not remove spaces if they are meaningful in the source.
  • Case differences: 'abc' and 'ABC' may compare as different values under the default case-sensitive behavior. Normalize case or choose a suitable collation when the application’s rules require case-insensitive matching.
  • Failed casts: Text with unexpected punctuation, blanks, or nonnumeric characters cannot always be converted to a number or date. Validate and clean the input, and use tolerant conversion approaches where available and appropriate.

A useful debugging habit is to inspect both the schema and representative values. Confirm whether a field is actually STRING, whether it contains nulls or whitespace, and whether a cast or ingestion step changed it. That usually identifies the cause more quickly than changing the table type without checking the data.

Performance and practical design choices

String columns can take more storage and processing than compact numeric or date columns, particularly when values are long or repeated. That is not a reason to encode meaningful text as an arbitrary number: choose the type that accurately represents the data. For large tables, avoid carrying unused long-text columns through every transformation, and select only the fields needed by a query.

Filtering, joining, or grouping on strings is common, but it can be affected by inconsistent formatting. For important join keys, standardize case and whitespace in a documented way, and use the same normalization on both sides. If a text field is really an identifier, preserve its exact representation rather than applying transformations that could merge distinct identifiers. For very long free-form text, consider whether the workload truly needs to sort or group by the entire value.

Frequently Asked Questions

Is STRING the same as VARCHAR in Databricks?
They are both text types, but VARCHAR(n) declares a maximum length while STRING does not declare a character limit in its type definition. Use the bounded form when that limit is an intentional part of the schema.

Does STRING mean a column can never be null?
No. Nullability is a separate property of the column or schema. A nullable string column can contain null, and a non-nullable column disallows it; an empty string remains a non-null value.

How do I cast a column to STRING?
In Databricks SQL, use CAST(column_name AS STRING). In PySpark, cast the column to 'string', for example with df.column_name.cast('string').

Should numeric-looking IDs use STRING?
Usually yes when the value identifies something rather than representing an amount. A string preserves leading zeroes and avoids unintended arithmetic, though numeric types are preferable for values that must be calculated or numerically ordered.

Is STRING case-sensitive?
Default string comparisons are generally case-sensitive, so differently capitalized values may not match. Databricks collation options can alter comparison behavior in supported environments; check the runtime and collation used by the table or expression.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.