SQL differences

LENGTH Counts Bytes on MySQL and MariaDB and Characters on PostgreSQL and SQLite

A check that a name is at most 20 characters long, written with LENGTH(name), lets 20 letters through on SQLite and PostgreSQL. MySQL and MariaDB count bytes instead, so a name with accents reaches the limit sooner.

In UTF-8 a letter outside A to Z takes two to four bytes. MySQL and MariaDB count characters with CHAR_LENGTH, which SQLite lacks. Length checks and cut-offs drift apart on names and addresses with accents.

SELECT LENGTH('é'), run on four engines: SQLite 3.45.1 1; PostgreSQL 16.15 1; MySQL 8.0.46 2; MariaDB 10.11.14 2.

What Each Engine Answered

The example

SELECT LENGTH('é');

gave these answers:

Engine Answer
SQLite 3.45.1 1
PostgreSQL 16.15 1
MySQL 8.0.46 2
MariaDB 10.11.14 2

What to Write Instead

No function counts characters on all four engines: use CHAR_LENGTH on PostgreSQL, MySQL and MariaDB and LENGTH on SQLite.

What BareSQL Portable Reports

Saved as example.sql, the example gets this from baresql lint --all:

$ baresql lint --all example.sql
example.sql:1:8: warning: LENGTH counts characters on SQLite and PostgreSQL and bytes on MySQL and MariaDB, so LENGTH('é') is 1 or 2. [length-bytes]
    LENGTH('é')
    No function counts characters on all four engines: use CHAR_LENGTH on PostgreSQL, MySQL and MariaDB and LENGTH on SQLite.

1 problem: 1 warning.

baresql rules length-bytes prints the same answers in the terminal, and the BareSQL Portable page lists every difference BareSQL knows.