---
title: ANSI_MODE
description: Control behaviors of built-in functions and cast operations with the ANSI\_MODE configuration parameter in Databricks SQL.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/parameters/ansi_mode
---

# ANSI\_MODE

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL

The `ANSI_MODE` configuration parameter controls key behaviors of built-in functions and cast operations.

This article describes ANSI mode in Databricks SQL. For ANSI compliance in Databricks Runtime, see [ANSI compliance in Databricks Runtime](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-ansi-compliance.md).

## Settings

* **TRUE**

  Follows the SQL standard in how it deals with certain arithmetic operations and type conversions, similar to most databases and data warehouses. Following this standard promotes better data quality, integrity, and portability.

* **FALSE**

  Databricks SQL uses Hive-compatible behavior.

You can set this parameter at the session level using the [SET statement](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-aux-conf-mgmt-set.md)
and at the global level using [SQL configuration parameters](https://docs.databricks.com/aws/en/admin/sql/index.md#params) or the [SQL Warehouse API](https://docs.databricks.com/api/workspace/warehouses/setworkspacewarehouseconfig).

## System default

For accounts created on or after October 19, 2022, the system default is `TRUE`.

## Detailed description

The Databricks SQL reference documentation describes SQL standard behavior.

The following sections describe the differences between ANSI\_MODE `TRUE` (ANSI mode) and `FALSE` (non-ANSI mode).

### Operators

In non-ANSI mode, arithmetic operations performed on numeric types may return overflowed values or NULL, while in ANSI mode such operations return an error.

| Operator                                                                                                       | Description                                                       | Example             | ANSI\_MODE = true | ANSI\_MODE = false |
| -------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | ------------------- | ----------------- | ------------------ |
| [dividend / divisor](https://docs.databricks.com/aws/en/sql/language-manual/functions/slashsign.md)            | Returns dividend divided by divisor.                              | `1/0`               | Error             | `NULL`             |
| [- expr](https://docs.databricks.com/aws/en/sql/language-manual/functions/minussignunary.md)                   | Returns the negated value of expr.                                | `-(-128y)`          | Error             | `-128y` (Overflow) |
| [expr1 - expr2](https://docs.databricks.com/aws/en/sql/language-manual/functions/minussign.md)                 | Returns the subtraction of expr2 from expr1.                      | `-128y - 1y`        | Error             | `127y` (Overflow)  |
| [expr1 + expr2](https://docs.databricks.com/aws/en/sql/language-manual/functions/plussign.md)                  | Returns the sum of expr1 and expr2.                               | `127y + 1y`         | Error             | `-128y` (Overflow) |
| [dividend % divisor](https://docs.databricks.com/aws/en/sql/language-manual/functions/percentsign.md)          | Returns the remainder after dividend / divisor.                   | `1 % 0`             | Error             | `NULL`             |
| [multiplier \* multiplicand](https://docs.databricks.com/aws/en/sql/language-manual/functions/asterisksign.md) | Returns multiplier multiplied by multiplicand.                    | `100y * 100y`       | Error             | `16y` (Overflow)   |
| [arrayExpr\[index\]](https://docs.databricks.com/aws/en/sql/language-manual/functions/bracketsign.md)          | Returns the element of an arrayExpr at index.                     | Invalid array index | Error             | `NULL`             |
| [mapExpr\[key\]](https://docs.databricks.com/aws/en/sql/language-manual/functions/bracketsign.md)              | Returns the value of mapExpr for key.                             | Invalid map key     | Error             | `NULL`             |
| [divisor div dividend](https://docs.databricks.com/aws/en/sql/language-manual/functions/div.md)                | Returns the integral part of the division of divisor by dividend. | `1 div 0`           | Error             | `NULL`             |

### Functions

The behavior of some built-in functions can be different under ANSI mode vs non-ANSI mode under the conditions specified below.

| Operator                                                                                                                   | Description                                                                | Condition                     | ANSI\_MODE = true | ANSI\_MODE = false |
| -------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | ----------------------------- | ----------------- | ------------------ |
| [abs(expr)](https://docs.databricks.com/aws/en/sql/language-manual/functions/abs.md)                                       | Returns the absolute value of the numeric value in expr.                   | `abs(-128y)`                  | Error             | `-128y` (Overflow) |
| [element\_at(mapExpr, key)](https://docs.databricks.com/aws/en/sql/language-manual/functions/element_at.md)                | Returns the value of mapExpr for key.                                      | Invalid map key               | Error             | `NULL`             |
| [element\_at(arrayExpr, index)](https://docs.databricks.com/aws/en/sql/language-manual/functions/element_at.md)            | Returns the element of an arrayExpr at index.                              | Invalid array index           | Error             | `NULL`             |
| [elt(index, expr1 \[, …\] )](https://docs.databricks.com/aws/en/sql/language-manual/functions/elt.md)                      | Returns the nth expression.                                                | Invalid index                 | Error             | `NULL`             |
| [make\_date(y,m,d)](https://docs.databricks.com/aws/en/sql/language-manual/functions/make_date.md)                         | Creates a date from year, month, and day fields.                           | Invalid result date           | Error             | `NULL`             |
| [make\_timestamp(y,m,d,h,mi,s\[,tz\])](https://docs.databricks.com/aws/en/sql/language-manual/functions/make_timestamp.md) | Creates a timestamp from fields.                                           | Invalid result timestamp      | Error             | `NULL`             |
| [make\_interval(y,m,w,d,h,mi,s)](https://docs.databricks.com/aws/en/sql/language-manual/functions/make_interval.md)        | Creates an interval from fields.                                           | Invalid result interval       | Error             | `NULL`             |
| [mod(dividend, divisor)](https://docs.databricks.com/aws/en/sql/language-manual/functions/mod.md)                          | Returns the remainder after dividend / divisor.                            | `mod(1, 0)`                   | Error             | `NULL`             |
| [next\_day(expr,dayOfWeek)](https://docs.databricks.com/aws/en/sql/language-manual/functions/next_day.md)                  | Returns the first date which is later than expr and named as in dayOfWeek. | Invalid day of week           | Error             | `NULL`             |
| [parse\_url(url, partToExtract\[, key\])](https://docs.databricks.com/aws/en/sql/language-manual/functions/parse_url.md)   | Extracts a part from url.                                                  | Invalid URL                   | Error             | `NULL`             |
| [pmod(dividend, divisor)](https://docs.databricks.com/aws/en/sql/language-manual/functions/pmod.md)                        | Returns the positive remainder after dividend / divisor.                   | `pmod(1, 0)`                  | Error             | `NULL`             |
| [size(expr)](https://docs.databricks.com/aws/en/sql/language-manual/functions/size.md)                                     | Returns the cardinality of expr.                                           | `size(NULL)`                  | `NULL`            | `-1`               |
| [to\_date(expr\[,fmt\])](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_date.md)                      | Returns expr cast to a date using an optional formatting.                  | Invalid expr or format string | Error             | `NULL`             |
| [to\_timestamp(expr\[,fmt\])](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_timestamp.md)            | Returns expr cast to a timestamp using an optional formatting.             | Invalid expr or format string | Error             | `NULL`             |
| [to\_unix\_timestamp(expr\[,fmt\])](https://docs.databricks.com/aws/en/sql/language-manual/functions/to_unix_timestamp.md) | Returns the timestamp in expr as a UNIX timestamp.                         | Invalid expr or format string | Error             | `NULL`             |
| [unix\_timestamp(\[expr\[, fmt\]\])](https://docs.databricks.com/aws/en/sql/language-manual/functions/unix_timestamp.md)   | Returns the UNIX timestamp of current or specified time.                   | Invalid expr or format string | Error             | `NULL`             |

### Casting rules

The rules and behaviors regarding CAST are stricter in ANSI mode. They can be divided into the following three categories:

* [Compile-time conversion rules](https://docs.databricks.com/aws/en/sql/language-manual/parameters/ansi_mode.md#compile-time-conversion-rules)
* [Runtime errors](https://docs.databricks.com/aws/en/sql/language-manual/parameters/ansi_mode.md#runtime-errors)
* [Implicit type coercion rules](https://docs.databricks.com/aws/en/sql/language-manual/parameters/ansi_mode.md#implicit-type-coercion-rules)

#### Compile-time conversion rules

| Source type      | Target type | Example                                            | ANSI\_MODE = true | ANSI\_MODE = false               |
| ---------------- | ----------- | -------------------------------------------------- | ----------------- | -------------------------------- |
| Boolean          | Timestamp   | `cast(TRUE AS TIMESTAMP)`                          | Error             | `1970-01-01 00:00:00.000001 UTC` |
| Date             | Boolean     | `cast(DATE'2001-08-09' AS BOOLEAN)`                | Error             | `NULL`                           |
| Timestamp        | Boolean     | `cast(TIMESTAMP'1970-01-01 00:00:00Z' AS BOOLEAN)` | Error             | `FALSE`                          |
| Integral numeric | Binary      | `cast(15 AS BINARY)`                               | Error             | binary representation            |

#### Runtime errors

| Source type        | Target type        | Condition     | Example                                   | ANSI\_MODE = true | ANSI\_MODE = false |
| ------------------ | ------------------ | ------------- | ----------------------------------------- | ----------------- | ------------------ |
| String             | Non-string         | Invalid input | `cast('a' AS INTEGER)`                    | Error             | `NULL`             |
| Array, Struct, Map | Array, Struct, Map | Invalid input | `cast(ARRAY('1','2','3') AS ARRAY<DATE>)` | Error             | `NULL`             |
| Numeric            | Numeric            | Overflow      | `cast(12345 AS BYTE)`                     | Error             | `NULL`             |
| Numeric            | Integral numeric   | Truncation    | `cast(5.1 AS INTEGER)`                    | Error             | `5`                |

> [!NOTE]
>
> For each of these casts you can use [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) instead of [cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/cast.md) to return `NULL` rather than of an error.

#### Implicit type coercion rules

Under `ANSI_MODE = TRUE`, Databricks SQL uses clear [SQL data type casting rules](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md) for:

* [type promotion](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md#type-promotion)
* [downcasting](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md#downcasting)
* [crosscasting](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md#crosscasting)

By contrast `ANSI_MODE = FALSE` is inconsistent and more lenient. For example:

* When using a `STRING` type with any arithmetic operator, the string is implicitly cast to `DOUBLE`.
* When comparing a `STRING` to any numeric type the string is implicitly cast to the type it compares to.
* When performing a `UNION`, `COALESCE`, or other operations where a least common type must be found all types are cast to `STRING` if there is any `STRING` type present.

Databricks recommends using the explicit [cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/cast.md) or [try\_cast](https://docs.databricks.com/aws/en/sql/language-manual/functions/try_cast.md) function instead of relying on `ANSI_MODE = FALSE`.

## Examples

```sql
> SET ansi_mode = true;

-- Protects against integral numeric overflow
> SELECT cast(12345 AS TINYINT);
  Casting 12345 to tinyint causes overflow

-- For invalid values raises errors instead of returning NULL.
> SELECT cast('a' AS INTEGER);
  Invalid input syntax for type numeric: a.
  To return NULL instead, use 'try_cast'

-- try_cast() is consistent for both modes
> SELECT try_cast('a' AS INTEGER);
  NULL

-- Does not allow ambiguous crosscasting.
> SELECT c1 + c2 FROM VALUES('5', '7.6') AS T(c1, c2);
  Cannot resolve '(T.c1 + T.c2)' due to data type mismatch:
  '(T.c1 + T.c2)' requires (numeric or interval day to second or interval year to month or interval) type, not string

-- Promotes STRING to least common type (STRING, INTEGER --> BIGINT) for arithmetic operation.
> SELECT typeof(5 - '3');
  bigint

-- Promotes STRING to least common type (INTEGER, STRING --> BIGINT) with runtime check
> SELECT c1 = c2 FROM VALUES(10, '10.1') AS T(c1, c2);
  Invalid input syntax for type numeric: 10.1. To return NULL instead, use 'try_cast'.

-- Promotes STRING to least common type (STRING, INTEGER --> BIGINT) for set operation with runtime check.
> SELECT typeof(c1) FROM (SELECT 5 UNION ALL SELECT '6') AS T(c1);
  bigint
  bigint
```

```sql
> SET ansi_mode = false;

-- Silent integral numeric overflow
> SELECT cast(12345 AS TINYINT);
  57

-- Returns NULL instead of an error
> SELECT cast('a' AS INTEGER);
  NULL

-- try_cast() is safe for both modes
> SELECT try_cast('a' AS INTEGER);
  NULL

-- Does allow ambiguous crosscasting using DOUBLE.
> SELECT c1 + c2 FROM VALUES('5', '7.6') AS T(c1, c2);
  12.6

-- Crosscasts STRING to DOUBLE for arithmetic operation.
> SELECT typeof(5 - '3');
  double

-- Implicitly casts STRING to INTEGER equating 10 with 10.1
> SELECT c1 = c2 FROM VALUES(10, '10.1') AS T(c1, c2);
  true

-- Promotes to string for set operations
> SELECT typeof(c1) FROM (SELECT 5 UNION ALL SELECT '6') AS T(c1);
  string
  string
```

## Related

* [SQL data type rules](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-datatype-rules.md)
* [RESET](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-aux-conf-mgmt-reset.md)
* [SET statement](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-aux-conf-mgmt-set.md)
* [Configure SQL parameters](https://docs.databricks.com/aws/en/admin/sql/index.md#params)
* [SQL Warehouse API](https://docs.databricks.com/api/workspace/warehouses/setworkspacewarehouseconfig)
