dataengref migrate

Home / SQL Server

SQL Server to Databricks: function mapping

13 of 20 common SQL Server functions need a change in Databricks. Mapping table with Databricks syntax and behavior differences.

Other targets: SQL Server to Snowflake · SQL Server to BigQuery

Needs a rewrite (7)

No Databricks function with the same behavior; use the construct shown.

SQL ServerDatabricksNotes
CONVERTRewrite CAST(expr AS type) / date_format(expr, 'yyyyMMdd')date_format uses Java-style datetime patterns: style 120 becomes 'yyyy-MM-dd HH:mm:ss'. Note MM is month and mm is minute.
Style codes only exist in SQL Server. Every CONVERT with a third argument needs a manual rewrite; a search for "CONVERT(" with three arguments finds them.
CONVERT(DATE, expr) on strings relies on SQL Server language settings to read the input. In the targets, parse strings with TO_DATE (Snowflake, Databricks) or PARSE_DATE (BigQuery) and an explicit format.
CROSS APPLYRewrite FROM t JOIN LATERAL (subquery)LATERAL subqueries can reference columns of preceding tables.
OUTER APPLY keeps left rows without matches. Translate it to a LEFT JOIN on the lateral or UNNEST form and test that rows with no match survive.
APPLY with TOP 1 ... ORDER BY inside (latest row per key) is often clearer as a window function: QUALIFY ROW_NUMBER() OVER (PARTITION BY key ORDER BY ts DESC) = 1, which all three targets support.
EOMONTHRewrite last_day(expr)Returns the last day of the month as a DATE.
EOMONTH(d, n) has no single-function equivalent. Write LAST_DAY(DATEADD(month, n, d)) in Snowflake, last_day(add_months(d, n)) in Databricks and LAST_DAY(DATE_ADD(d, INTERVAL n MONTH)) in BigQuery.
FORMATRewrite date_format(expr, 'yyyy-MM-dd')Java-style patterns, close to .NET for common date formats. Use format_number for numbers.
Culture-dependent output (month names, decimal separators) has no direct equivalent; the targets format in English. Check any FORMAT call that passes a culture such as 'de-DE'.
Standard .NET format codes such as 'd', 'N2' or 'C' must be written out as explicit patterns in the target.
PATINDEXRewrite regexp_instr(str, regexp)Databricks SQL and Databricks Runtime 11.3 LTS and above.
Translate the LIKE pattern: drop the outer %, turn _ into ., keep [0-9] classes. PATINDEX('%[0-9]%', s) becomes REGEXP_INSTR(s, '[0-9]').
SQL Server matching follows the column collation, often case-insensitive; regular expressions in the targets are case-sensitive unless a flag or (?i) is used.
STRING_SPLITRewrite LATERAL VIEW explode(split(str, regex))split() treats the delimiter as a regular expression, so characters such as | or . must be escaped.
split(tags, '|') splits on every character because | is regex alternation; write split(tags, '\\|') instead.
STRING_SPLIT does not guarantee output order unless enable_ordinal = 1 (SQL Server 2022). Use the position column of the target when order matters.
TOPRewrite SELECT ... LIMIT nLIMIT goes at the end of the query.
TOP n PERCENT and TOP n WITH TIES need a rewrite with window functions, for example QUALIFY RANK() OVER (ORDER BY score DESC) <= n for WITH TIES (QUALIFY works in all three targets).
TOP in UPDATE and DELETE statements is SQL Server only; rewrite those with a key filter.

Different name (6)

Same behavior under another name; rename the call and check the notes.

SQL ServerDatabricksNotes
DATEFROMPARTSDifferent name make_date(year, month, day)Invalid dates return NULL or an error depending on ANSI mode.
DATEPARTDifferent name date_part(fieldStr, expr)Synonym of extract(field FROM expr). The field is a string such as 'YEAR' or 'DOW'.
DATEPART(weekday, d) follows SET DATEFIRST (Sunday = 1 with the US English default). Databricks DOW and BigQuery DAYOFWEEK also return Sunday = 1; Snowflake depends on the WEEK_START session parameter. Check any code that compares weekday numbers.
DATEPART(week, d) counts weeks starting January 1 and follows DATEFIRST; ISO weeks are DATEPART(iso_week, d). Map each one to the matching ISO or non-ISO week part of the target.
IIFDifferent name iff(cond, expr1, expr2)Synonym of if(cond, expr1, expr2).
A NULL condition returns the false branch in all three targets, the same as SQL Server. Nested IIF calls translate one to one, but a CASE expression is easier to read when there are more than two branches.
ISNULLDifferent name ifnull(expr1, expr2)nvl(expr1, expr2) is a synonym. Both are documented as coalesce with two arguments. The built-in isnull(expr) only returns true or false.
Return type: SQL Server returns the type of the first argument and may truncate the replacement, for example ISNULL(CAST(NULL AS VARCHAR(2)), 'abc') returns 'ab'. The target functions return a common type of both arguments, so results that relied on truncation change.
A query that keeps ISNULL(col, 0) fails on Databricks because isnull takes one argument. A query that uses ISNULL(col) as a condition still runs but means something else.
NEWIDDifferent name uuid()Returns a random UUID as a STRING.
ORDER BY NEWID() is a common way to shuffle rows. In the targets, order by RANDOM() (Snowflake), rand() (Databricks) or RAND() (BigQuery) instead of generating UUIDs.
Columns typed uniqueidentifier become plain strings. Comparisons that relied on the SQL Server type (for example ordering by a uniqueidentifier) behave differently.
STUFFDifferent name overlay(input PLACING replace FROM pos FOR len)Also callable as overlay(input, replace, pos, len); note that replace comes before pos.
The common pattern STUFF((SELECT ',' + name FROM ... FOR XML PATH('')), 1, 1, '') only removes the leading comma of an XML-built list. Replace the whole expression with LISTAGG (Snowflake), string_agg or listagg (Databricks) or STRING_AGG (BigQuery). See STRING_AGG.
SQL Server STUFF returns NULL when start is larger than the string length. Check how each target handles positions past the end before relying on it.

Works as written (7)

The same call runs, but check the notes for behavior differences.

SQL ServerDatabricksNotes
CHARINDEXSame name charindex(substr, str [, pos])Synonym of locate; 1-based, returns 0 when not found.
SQL Server comparisons follow the column collation, which is often case-insensitive. The targets compare case-sensitively by default, so CHARINDEX('abc', 'ABC') stops matching.
DATEADDSame name dateadd(unit, value, expr)Synonym of timestampadd, available in Databricks Runtime 10.4 LTS and above. Returns a TIMESTAMP. The two-argument dateadd(startDate, numDays) form adds days to a DATE.
Month arithmetic at month end: SQL Server returns the last day of the target month when the day does not exist (Jan 31 + 1 month = Feb 28 or 29). Snowflake DATEADD, Databricks and BigQuery behave the same way for this case.
The three-argument dateadd returns a TIMESTAMP even for DATE input; cast back with CAST(... AS DATE) if a date column is expected.
DATEDIFFSame name datediff(unit, start, end)Synonym of timestampdiff (Databricks Runtime 10.4 LTS and above). Counts whole elapsed units: one month has passed only when the day and time reach the start value. The two-argument datediff(endDate, startDate) form returns days and takes the end date first.
Databricks documents that datediff(MONTH, TIMESTAMP '2021-02-28 12:00:00', TIMESTAMP '2021-03-28 11:59:59') returns 0 because a full month has not elapsed, while SQL Server returns 1 because a month boundary was crossed. Month, quarter and year differences are the most common source of mismatched reports after migration.
datediff(a, b) with two arguments is end minus start in days, the reverse of the three-argument form.
GETDATESame name getdate()Synonym of current_timestamp(). Evaluated once at the start of the query.
SQL Server GETDATE returns the local time of the database server as DATETIME with no time zone. Cloud warehouses default to UTC or the session time zone, so day boundaries and "today" filters can shift by several hours. Set the session time zone or pass one explicitly.
LENSame name len(expr)Synonym of char_length (Databricks Runtime 11.3 LTS and above). Counts trailing spaces.
SQL Server LEN('abc ') returns 3; the targets return 5. Wrap the argument in RTRIM to keep the old result, for example LENGTH(RTRIM(col)).
Columns migrated from CHAR(n) may arrive padded with spaces, which makes the difference more common than expected.
STRING_AGGSame name string_agg([DISTINCT] expr [, delimiter]) [WITHIN GROUP (ORDER BY ...)]Databricks SQL and Databricks Runtime 16.4 and later; alias of listagg. On older runtimes use array_join(collect_list(expr), delimiter), which does not guarantee order.
The separator is optional in the targets and defaults to an empty string, while SQL Server requires it. Keep it explicit to avoid values running together.
SQL Server returns an error when the result exceeds 8,000 bytes for VARCHAR input unless the input is cast to VARCHAR(MAX). The targets have their own size limits but no such cast is needed.
TRY_CASTSame name try_cast(sourceExpr AS targetType)Databricks SQL and Databricks Runtime 10.4 LTS and above.
TRY_CONVERT with a style code (for example TRY_CONVERT(date, s, 103)) needs a parse function with an explicit format instead: TRY_TO_DATE in Snowflake, try_to_date in Databricks, SAFE.PARSE_DATE in BigQuery.

Paste a query into the SQL checker to list the functions it uses that need a change.