// strings reference

SQL PARSENAME function

Returns one part of a four-part dotted object name, counting from the right.

Kategorie
Strings
Returns
string or NULL
In this category
105 of 172

Syntax

PARSENAME(name, part)
part: 1 = object, 2 = schema, 3 = database, 4 = server

Each block is a separate form. The support table and the examples show which engine takes which.

Engine-Unterstützung

EngineSupportNotizen
SQL Server Ja Supported under this name.
PostgreSQL Not built in No PARSENAME; SPLIT_PART or parse_ident for qualified names.
MySQL Not built in No PARSENAME; SUBSTRING_INDEX.
SQLite Not built in No PARSENAME; INSTR arithmetic with SUBSTR.
Oracle Not built in No PARSENAME; REGEXP_SUBSTR with occurrence counts.

Beispiele

SQL Server

SELECT PARSENAME('db.dbo.orders', 1) AS object_name;  -- orders

SQL Server

SELECT PARSENAME('10.20.30.40', 4) AS first_octet;  -- 10

Gut zu wissen

  • Parts are counted from the right: PARSENAME('db.dbo.orders', 1) is 'orders', part 2 is 'dbo', part 3 is 'db', and part 4 is NULL.
  • Five or more dot-separated segments return NULL for every part, so PARSENAME is unreliable for arbitrary strings like a.b.c.d.e.
  • It understands bracketed parts: PARSENAME('[dbo].[Order Header]', 2) strips the brackets and returns dbo.

Questions about PARSENAME

Can PARSENAME split any dotted string?

Only up to four segments, because it was built for server.database.schema.object names. A fifth segment makes it return NULL; use STRING_SPLIT with the ordinal argument for general splitting.

Why do people use PARSENAME for IP addresses and file names?

An IPv4 address has exactly four parts, so PARSENAME(ip, 4) through PARSENAME(ip, 1) read the octets. For file names, part 1 is the extension, until a name contains more than three dots.

What does PARSENAME('orders', 2) return?

NULL. With no schema segment present there is no part 2; only part 1 exists.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.