Muokkaa

AI_TRANSLATE (Transact-SQL)

Applies to: SQL analytics endpoint in Microsoft Fabric and Warehouse in Microsoft Fabric

AI_TRANSLATE translates input text into a target language.

Note

  • AI_TRANSLATE is in preview.
  • AI_TRANSLATE is available only in SQL analytics endpoint and Warehouse in Microsoft Fabric.

Syntax

Transact-SQL syntax conventions

AI_TRANSLATE ( text, lang_code [ (NULL | ERROR | DEFAULT <value>) ON ERROR ] )

Arguments

text

An expression of a character type, for example nvarchar, varchar, nchar, or char.

lang_code

Language code for the translation target.

Supported values: de, en, fr, it, es, el, pl, sv, fi, cs.

ON ERROR

The ON ERROR clause controls how an AI function handles processing errors.

  • NULL ON ERROR returns NULL when the function can't process a value. This is the default behavior and doesn't need to be explicitly specified.
  • ERROR ON ERROR causes the entire query to fail if an error occurs while processing any input value.
  • DEFAULT <value> ON ERROR returns the specified default value instead of NULL when an error occurs.

Errors can be caused by Responsible AI safety checks, input size limits, transient service issues, or other processing failures.

Return types

Returns nvarchar(max) with translated text.

Remarks

AI functions return NULL if the AI model can't process the text. Common reasons include:

  • Responsible AI rules block inappropriate content in the input text.
  • Input text exceeds token limits. The current model supports up to 15 KB of text.

Examples

A. Translate to German

SELECT ai_translate('The hotel was great', 'de') AS translation_de;

Expected result: Das Hotel war großartig.

B. Translate review text into multiple languages

SELECT review_id,
       ai_translate(review_text, 'de') AS review_de,
       ai_translate(review_text, 'fr' ERROR ON ERROR) AS review_fr,
       ai_translate(review_text, 'es' DEFAULT 'N/A' ON ERROR) AS review_es
FROM dbo.hotel_reviews;

If translation to German fails, the first function will return NULL instead of error.

The NULL ON ERROR clause instructs AI function that translates text to French to fail the query if any input value cannot be translated. The DEFAULT 'N/A' ON ERROR clause instructs AI function that translates text to Spanish to return the value N/A for every input value that cannot be processed.