SQL Keywords

SQL NAMES

What does the SQL SET NAMES statement do?

SET NAMES changes the client-server character set and optional collation for the current connection.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL NAMES: MySQL, MariaDB, PostgreSQL (as alias for SET client_encoding). Not supported in SQL Server, Oracle, or SQLite.

SQL NAMES Full Explanation

The NAMES keyword appears in the SET statement (SET NAMES) to tell the database which character set the client is using and, optionally, which collation should apply to string literals sent during the current session. When executed, the server adjusts the variables that govern parsing and result encoding so that text is interpreted and returned correctly. In MySQL and MariaDB, SET NAMES simultaneously updates character_set_client, character_set_connection, and character_set_results. PostgreSQL treats SET NAMES as a shorthand for SET client_encoding. Changing the connection character set does not convert existing stored data; it only affects how incoming and outgoing text is encoded for this session. The command is transaction-independent and takes effect immediately.

SQL NAMES Syntax

SET NAMES 'character_set_name';
SET NAMES 'character_set_name' COLLATE 'collation_name';

SQL NAMES Parameters

  • collation_name - STRING (Optional) Specific collation from the chosen character set.

Example Queries Using SQL NAMES

-- Switch the session to UTF8
SET NAMES 'utf8';

-- Use UTF8 with an explicit collation
SET NAMES 'utf8' COLLATE 'utf8_general_ci';

-- PostgreSQL equivalent
SET NAMES 'LATIN1';

Expected Output Using SQL NAMES

  • The server updates session variables governing character set (and collation if specified)
  • Subsequent queries will encode and decode text using the designated character set

Use Cases with SQL NAMES

  • Align encoding when the client application uses a different character set than the server default.
  • Prevent mojibake when importing CSV files created in Windows-1252 or Shift_JIS.
  • Ensure that stored procedures emitting JSON use UTF8 consistently.
  • Quickly test collation-specific comparison behavior without altering permanent server settings.

Common Mistakes with SQL NAMES

  • Omitting quotes around the character set name (SET NAMES utf8; is invalid in some clients).
  • Assuming SET NAMES converts data already stored in tables – it only affects the session, not the underlying data.
  • Forgetting to specify COLLATE when a non-default collation is required, leading to unexpected sort order.
  • Using unsupported or misspelled character set names, which raises an error.

Related Topics

SET character_set_client, SET client_encoding, CHARACTER SET, COLLATE, SHOW CHARACTER SET

First Introduced In

MySQL 4.1.0 (2003)

Frequently Asked Questions

Does SET NAMES change data already stored?

No. It only affects how text is sent to and retrieved from the server during the current session.

Can I specify a collation with SET NAMES?

Yes. Append COLLATE 'collation_name' to control comparison and ordering rules.

Is SET NAMES portable across databases?

It is supported in MySQL, MariaDB, and PostgreSQL (as an alias). SQL Server, Oracle, and SQLite do not recognize it.

What happens if I omit quotes around the character set?

Many clients require quotes. Omitting them may raise a syntax error or treat the identifier as a column name.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!