AI_EXTRACT#

Extracts the named entities from the input text and returns them as a flat JSON object whose keys are the requested entity names.

Syntax#

AI_EXTRACT ( text_expression, entity [ , ...n ] )

Arguments#

text_expression

The text to extract from. Any character expression (char, varchar, nchar or nvarchar).

entity [ , …n ]

One or more names of the fields to extract.

Return types#

nvarchar(max) — a flat JSON object with one key per requested entity, values as strings.

Remarks#

The function executes in Querona against the session’s ambient AI model (see AI (generative)) and works over data from any connected source. A failed call — provider error, content restriction, or input over roughly 15 KB — returns NULL for that row without aborting the query; an authorization failure (for example a rejected API key) raises a statement-level error instead. The function is non-deterministic and is re-evaluated on every execution.

Pair the result with OPENJSON to shred the extracted fields into columns.

Example#

SELECT r.id, j.sentiment, j.problem
FROM   hotel_reviews AS r
CROSS APPLY OPENJSON(AI_EXTRACT(r.review_text, 'sentiment', 'problem'))
       WITH (sentiment varchar(100), problem varchar(1000)) AS j;

See Also#