Advent of 2025, Day 13 – SQL Server 2025 – Vector functions

In this Microsoft SQL Server 2025 series:

  1. Dec 01: Microsoft SQL Server 2025 – Introduction and installation
  2. Dec 02: Microsoft SQL Server 2025 – New T-SQL functions – native JSON data type and some functions
  3. Dec 03: Microsoft SQL Server 2025 – New T-SQL functions – JSON Index
  4. Dec 04: Microsoft SQL Server 2025 – New T-SQL functions – Product()
  5. Dec 05: Microsoft SQL Server 2025 – New T-SQL functions – BASE64_ENCODE() and BASE64_DECODE()
  6. Dec 06: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_LIKE()
  7. Dec 07: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_SUBSTR() and REGEXP_REPLACE()
  8. Dec 08: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_INSTR() and REGEXP_COUNT()
  9. Dec 09: Microsoft SQL Server 2025 – New T-SQL functions – REGEXP_MATCHES() and REGEXP_SPLIT_TO_TABLE()
  10. Dec 10: Microsoft SQL Server 2025 – External REST endpoint invocation
  11. Dec 11: Microsoft SQL Server 2025 – External REST endpoint invocation using LLM
  12. Dec 12: Microsoft SQL Server 2025 – New vector data type and Vector functions

Yesterday we looked into Vector data type and how to create table, insert the vector and read it. With SQL Server 2025, vector data type comes equipped also with couple of functions:

  • Vector_Distance
  • Vector_Norm
  • Vector_Normalize
  • Vectorproperty
  • Vector_Search

Let’s look into couple of them.

VECTOR_DISTANCE()

The function calculates the distance between two vectors using a specified distance metric. Vector distance is always exact and doesn’t use any vector index, even if available.

The pseudo-code: VECTOR_DISTANCE ( distance_metric , vector1 , vector2 )

A string with the name of the distance metric to use to calculate the distance between the two given vectors. The following distance metrics are supported:

  • cosine – Cosine distance; range: 0-2 (0= Identical; 2=opposing)
  • euclidean – Euclidean distance; range: 0 – infinity (0=Identical)
  • dot – (Negative) Dot product ; range neg.inf – pos.infinity (smaller number indicate similar vectors obtained by calculating the negative dot product)
DECLARE @v1 AS VECTOR(2) = '[1,1]';
DECLARE @v2 AS VECTOR(2) = '[-1,-1]';
DECLARE @v3 AS VECTOR(2) = '[1,1]';

SELECT 
 VECTOR_DISTANCE('euclidean', @v1, @v2) AS euclidean_v1_v2
,VECTOR_DISTANCE('cosine', @v1, @v2) AS cosine_v1_v2
,VECTOR_DISTANCE('dot', @v1, @v2) AS negative_dot_product_v1_v2
,VECTOR_DISTANCE('euclidean', @v1, @v3) AS euclidean_v1_v3
,VECTOR_DISTANCE('cosine', @v1, @v3) AS cosine_v1_v3
,VECTOR_DISTANCE('dot', @v1, @v3) AS negative_dot_product_v1_v3

With the results:

VECTOR_NORM()

Use VECTOR_NORM to take a vector as an input and return the norm of the vector (which is a measure of its length or magnitude) in a given norm_type.

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.

Or on a different way to define the “length”:

  • norm2 (most common for embeddings): square root of sum of squares.
  • norm1: sum of absolute values: v1=v1+v2+\|v\|_1 = |v_1| + |v_2| + \dots∥v∥1​=∣v1​∣+∣v2​∣+…
  • norm: maximum absolute value: v=max(vi)\|v\|_\infty = \max(|v_i|)∥v∥∞​=max(∣vi​∣)
DECLARE @v4 AS VECTOR(6) = '[1, 2, 3, 4, 6, 5]';

SELECT 
  VECTOR_NORM(@v4, 'norm1') AS norm1
 ,VECTOR_NORM(@v4, 'norm2') AS norm2
 ,VECTOR_NORM(@v4, 'norminf') AS norminf

It calculates the values of the sum of absolute values of the vector components (norm1), the square root of the sum of the squares of the vector components (norm2) and the maximum of the absolute values of the vector components (norminf).

VECTOR_NORMALIZE()

On one hand VECTOR_NORM, calculates s singlular value, where as VECTOR_NORMALIZE take a vector and returns the normalized vector, which is a vector scaled to have a length of 1.

This means that the function rescales a vector so its magnitude becomes 1, while keeping its direction the same. Normalize simply means: divide every component by the vector’s length.

Naming between NORM and NORMALIZE can be slightly confusing, but both have a different effect and role. So be careful.

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


SELECT 
       VECTOR_NORMALIZE(@v7, 'norm1') AS V7_norm1
       ,VECTOR_NORMALIZE(@v7, 'norminf') AS V7_NormInf

NormInf for the vector V7 is a JSON with these numbers:

[
  3.3333334e-001,
  6.6666669e-001,
  1.0000000e+000
]

VECTOR_SEARCH()

The function searches for vectors similar to a given query vectors using an approximate nearest neighbors vector search algorithm.

I am creating a new database and and altering the database scoped configuration to set the preview features on (just in case; it is still the subject of change, as the feature is still in preview – by the time of writing this blog post).

 -- VECTOR_SEARCH
 USE MASTER;
 GO

 CREATE DATABASE db_11_vector;
 GO

 USE db_11_vector;
 GO
 
 -- optional
ALTER DATABASE SCOPED CONFIGURATION
SET PREVIEW_FEATURES = ON;
GO

Creating a table with a vector, that we call embeddings; these will later be used for calculating the similarties. Please note; embeddings are mocked data (!)

CREATE TABLE dbo.Aoc_days
(
    id INT PRIMARY KEY,
    title NVARCHAR(100),
    content NVARCHAR(MAX),
    embedding VECTOR(6) -- I will add some mocked embeddings
);
GO

Now we will add some mocked embeddings to see how the search function works.

INSERT INTO dbo.Aoc_days (id, title, content, embedding)
VALUES
  (1,  'Day 1: Secret Entrance',
       'Simulate a rotating dial from a sequence of turn commands; count how often you land on zero (and, in the second part, how often you cross zero while turning).',
       '[0.558, 0.646, 0.478, 0.173, 0.000, 0.999]'),

  (2,  'Day 2: Gift Shop',
       'Validate product IDs by detecting repeated patterns; part two broadens the repetition rule to catch more invalid IDs.',
       '[0.464, 0.950, 0.023, 0.693, 0.844, 0.536]'),

  (3,  'Day 3: Lobby',
       'Choose digits from multiple battery banks to maximize a joltage value under ordering constraints; part two scales up the selection size and tightens constraints.',
       '[0.072, 0.189, 0.662, 0.050, 0.243, 0.234]'),

  (4,  'Day 4: Printing Department',
       'Work on a grid of paper rolls: count which rolls are reachable under adjacency rules, then simulate a removal process to compute the total removed.',
       '[0.439, 0.617, 0.943, 0.945, 0.409, 0.025]'),

  (5,  'Day 5: Cafeteria',
       'Compare available ingredient IDs to freshness ranges: count IDs inside ranges, then merge overlapping ranges and compute the total unique fresh IDs.',
       '[0.318, 0.566, 0.744, 0.527, 0.210, 0.730]'),

  (6,  'Day 6: Trash Compactor',
       'Parse vertically-stacked arithmetic problems and evaluate them; part two changes how you read the columns (right-to-left / column-wise) before solving.',
       '[0.905, 0.912, 0.508, 0.705, 0.253, 0.935]'),

  (7,  'Day 7: Laboratories',
       'Trace beam paths through splitters and obstacles; part two counts the number of resulting timelines/paths efficiently using dynamic programming.',
       '[0.446, 0.127, 0.219, 0.736, 0.236, 0.395]'),

  (8,  'Day 8: Playground',
       'Connect floating junction boxes using the shortest total cable length (graph MST style); part two adds additional constraints requiring careful component tracking.',
       '[0.047, 0.232, 0.145, 0.114, 0.944, 0.325]'),

  (9,  'Day 9: Movie Theater',
       'Analyze a tile grid to find maximum-area rectangles under corner/marker constraints; part two extends the geometry logic with more complex inclusion rules.',
       '[0.059, 0.341, 0.897, 0.057, 0.815, 0.342]'),

  (10, 'Day 10: Factory',
       'Configure machines by pressing button pairs that toggle multiple indicators; find the minimum presses to reach target states, then aggregate results across many machines.',
       '[0.327, 0.857, 0.555, 0.290, 0.000, 0.000]'),

  (11, 'Day 11: Reactor',
       'Interpret device outputs as a network; compute connectivity and path counts between key components to restore communication between the server rack and reactor.',
       '[0.812, 0.116, 0.571, 0.115, 0.944, 0.623]'),

  (12, 'Day 12: Christmas Tree Farm',
       'Optimize a constrained arrangement/packing problem among many trees; brute force fails, so you derive a more efficient strategy to compute the final score.',
       '[0.585, 0.940, 0.231, 0.116, 0.977, 0.322]');
GO

-- create a vector
CREATE VECTOR INDEX vec_idx ON Aoc_days(embedding)
WITH (METRIC = 'cosine', TYPE = 'diskann');
GO


-- peform erform a vector similarity search
DECLARE @aoc VECTOR(6) = '[0.812, 0.235, 0.625, 0.011, 0.952, 0.043]';

SELECT
    a.id,
    a.title,
    a.content,
    s.distance
FROM
    VECTOR_SEARCH(
        TABLE = Aoc_days AS a,
        COLUMN = embedding,
        SIMILAR_TO = @aoc,
        METRIC = 'cosine',
        TOP_N = 4
    ) AS s
ORDER BY s.distance, a.title;

And the results are showing the use of VECTOR_SEARCH with use of embeddings and similarity function.

VECTORPROPERTY()

The VECTORPROPERTY function returns specific properties of a given vector. As for now, it can return two properties; Dimensions – vector’s dimension count and BaseType – vector’s base type.

For example:

DECLARE @v6 AS VECTOR(4) = '[1.043,2043.1234,-205.043, 0.000034234]';
SELECT 
   VECTORPROPERTY(@v6, 'Dimensions') AS VectorDimension
  ,VECTORPROPERTY(@v6 ,'BaseType') AS VectorType

Tomorrow we will look into the new T-SQL functions to work LLMs and Vector database.

As always, the code is available at my Github: https://github.com/tomaztk/SQLServer2025

Happy coding!

Tagged with: , , , , , , , ,
Posted in SQL Server, Uncategorized
11 comments on “Advent of 2025, Day 13 – SQL Server 2025 – Vector functions
  1. […] Dec 13: Microsoft SQL Server 2025 – Vector functions […]

    Like

  2. […] Dec 13: Microsoft SQL Server 2025 – Vector functions […]

    Like

  3. […] Dec 13: Microsoft SQL Server 2025 – Vector functions […]

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

William Durkin

William Durkin a blog on SQL Server, Replication, Performance Tuning and whatever else.

$hell Your Experience !!!

As aventuras de um DBA usando o Poder do $hell

Design a site like this with WordPress.com
Get started