Skip to content

Latest commit

 

History

History
115 lines (84 loc) · 3.44 KB

File metadata and controls

115 lines (84 loc) · 3.44 KB
title VECTOR_NORM (Transact-SQL)
description VECTOR_NORM takes a vector as an input and returns the norm of the vector (which is a measure of its length or magnitude) in a given norm type.
author WilliamDAssafMSFT
ms.author wiassaf
ms.reviewer damauri, pookam
ms.date 02/18/2025
ms.service sql
ms.subservice t-sql
ms.topic reference
f1_keywords
VECTOR_NORM
VECTOR_NORM_TSQL
helpviewer_keywords
VECTOR_NORM function
vector, norm calculation
dev_langs
TSQL
monikerRange = azuresqldb-current
ms.collection ce-skilling-ai-copilot
ms.custom
build-2025

VECTOR_NORM (Transact-SQL) (Preview)

[!INCLUDE Azure SQL Database]

Note

This data type is in preview and is subject to change. Make sure to read preview usage terms in the Service Level Agreements (SLA) for Online Services document.

Takes a vector as an input and returns the norm of the vector (which is a measure of its length or magnitude) in a given norm type.

For example, if you want to calculate the Euclidean norm (which is the most common norm type), you can use:

SELECT VECTOR_NORM ( vector, 'norm2' )
FROM ...

Syntax

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

VECTOR_NORM ( vector, norm_type )

Arguments

vector

An expression that evaluates to vector data type.

norm_type

A string with the name of the norm type to use to calculate the norm of the given vector. The following norm types are supported:

  • norm1 - The 1-norm, which is the sum of the absolute values of the vector components.
  • norm2 - The 2-norm, also known as the Euclidean Norm which is the square root of the sum of the squares of the vector components.
  • norminf - The infinity norm, which is the maximum of the absolute values of the vector components.

Return value

The function returns a float value that represents the norm of the vector using the specified norm type.

An error is returned if norm_type isn't a valid norm type and if the vector is not of the vector data type.

Examples

Example 1

The following example creates a vector with three dimensions from a string with a JSON array.

DECLARE @v VECTOR(3) = '[1, 2, 3]';

SELECT 
    vector_norm(@v, 'norm2') AS norm2,
    vector_norm(@v, 'norm1') AS norm1,
    vector_norm(@v, 'norminf') AS norminf;

The expected return values would be:

norm2 norm1 norminf
3.7416573867739413 6.0 3.0

Example 2

The following example calculates the norm of each vector in a table.

CREATE TABLE dbo.vectors
(
  ID INT PRIMARY KEY,
  v VECTOR(3) NOT NULL
);

INSERT INTO dbo.vectors (ID, v) VALUES 
(1, '[0.1, -2, 42]'),
(2, '[2, 0.1, -42]');

SELECT 
  ID, 
  VECTOR_NORM(v, 'norm2') AS norm 
FROM 
  dbo.vectors;

Related content