Vector Functions for Semantic Search
This lesson explains VECTOR_DISTANCE, VECTOR_NORMALIZE, VECTOR_NORM, VECTORPROPERTY and VECTOR_SEARCH.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish between the cosine, euclidean and dot metrics.
- Explain when to normalize vectors.
- Choose between VECTOR_DISTANCE and VECTOR_SEARCH.
VECTOR_DISTANCE and the metrics
VECTOR_DISTANCE(metric, v1, v2) returns the exact distance between two vectors of the same dimensions; smaller always means more similar. The three metrics behave as the following table describes.
| Metric | Measures | Range |
|---|---|---|
| cosine | Angle between vectors, ignoring length | 0 (same direction) to 2 (opposite) |
| euclidean | Straight-line distance, including length | 0 and up |
| dot | Negative dot product | Any value; more negative is more similar |
Used with TOP (k) ... ORDER BY, VECTOR_DISTANCE performs an exact k-nearest neighbour search over every candidate row.
DECLARE @q vector(1536) = AI_GENERATE_EMBEDDINGS(N'lightweight tent for two' USE MODEL EmbeddingModel);
SELECT TOP (5) p.ProductID, p.Name,
VECTOR_DISTANCE('cosine', e.Embedding, @q) AS distance
FROM Catalog.ProductEmbedding AS e
JOIN Catalog.Product AS p ON p.ProductID = e.ProductID
WHERE p.CategoryID = 3
ORDER BY distance;The following diagram contrasts the three metrics.

Distance metrics
VECTOR_NORMALIZE, VECTOR_NORM and VECTORPROPERTY
VECTOR_NORMALIZE(v, 'norm2') returns a vector in the same direction scaled to length 1 (also 'norm1' or 'norminf'). For normalized vectors, cosine distance and dot product produce the same ranking, and dot product is cheaper, so normalizing vectors from models that do not already normalize them lets queries use the dot metric. Azure OpenAI embeddings are already normalized. VECTOR_NORM(v, 'norm2') returns a vector's length, which shows whether vectors are normalized. VECTORPROPERTY(v, 'Dimensions') and VECTORPROPERTY(v, 'BaseType') describe a vector, which helps validate that stored vectors match the expected model and precision.
DECLARE @v vector(3) = '[3, 4, 0]';
SELECT VECTOR_NORM(@v, 'norm2') AS Length, -- 5
VECTOR_NORMALIZE(@v, 'norm2') AS UnitVector, -- [0.6, 0.8, 0]
VECTORPROPERTY(@v, 'Dimensions') AS Dims, -- 3
VECTORPROPERTY(@v, 'BaseType') AS BaseType; -- float32Norms and properties
VECTOR_SEARCH
VECTOR_SEARCH is a table-valued function that searches a table's vector column for the vectors most similar to a query vector. It returns all columns of the table plus a distance column. With SELECT TOP (N) WITH APPROXIMATE ... ORDER BY distance it uses a vector index whose metric matches (approximate search); without WITH APPROXIMATE, TOP (N) ... ORDER BY distance performs exact kNN. The columns of the searched table are referenced through the alias given in the TABLE argument.
SELECT TOP (10) WITH APPROXIMATE
e.ProductID, r.distance
FROM VECTOR_SEARCH(
TABLE = Catalog.ProductEmbedding AS e,
COLUMN = Embedding,
SIMILAR_TO = @q,
METRIC = 'cosine') AS r
ORDER BY r.distance;The choice of function depends on the task, as the following comparison summarizes.
VECTOR_DISTANCE or VECTOR_SEARCH?
Dot product on unnormalized vectors
A team stores embeddings from an open-source model that does not normalize its output and wants to use the dot metric for speed. Rankings with dot differ from cosine rankings, favouring long documents. What should the developer do?
Normalize the stored vectors and the query vector with VECTOR_NORMALIZE(..., 'norm2'); for unit-length vectors, dot product ranks identically to cosine. Without normalization, the dot product is influenced by vector length, which is why long documents rank higher. Switching to euclidean distance would still be affected by length.
For vectors normalized to length 1, how do cosine and dot product rankings compare?
Show answer
They are identical, so the cheaper dot product can be used.
Exam Tips
Remember for the exam
- Exact distance: VECTOR_DISTANCE; smaller is more similar for every metric.
- Normalized vectors: cosine and dot give the same ranking.
- Validate dimensions and precision: VECTORPROPERTY.
- Index-backed approximate search: VECTOR_SEARCH with TOP (N) WITH APPROXIMATE.