SnowConvert AI - SQL Server-Azure Synapse - Built-in functions¶
Applies to
SQL Server
Azure Synapse Analytics
Note
For more information about built-in functions and their Snowflake equivalents, also see Common built-in functions.
Aggregate¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
TransactSQL |
Snowflake |
Notes |
APPROX_COUNT_DISTINCT |
APPROX_COUNT_DISTINCT |
|
AVG |
AVG |
|
CHECKSUM_AGG |
*to be defined |
|
COUNT |
COUNT |
|
COUNT_BIG |
*to be defined |
|
GROUPING |
GROUPING |
|
GROUPING_ID |
GROUPING_ID |
|
MAX |
MAX |
|
MIN |
MIN |
|
STDEV |
STDDEV, STDEV_SAMP |
|
STDEVP |
STDDEV_POP |
|
SUM |
SUM |
|
VAR |
VAR_SAMP |
|
VARP |
VAR_POP |
Analytic¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
CUME_DIST |
CUME_DIST |
|
FIRST_VALUE |
FIRST_VALUE |
|
LAG |
LAG |
|
LAST_VALUE |
LAST_VALUE |
|
LEAD |
LEAD |
|
PERCENTILE_CONT |
PERCENTILE_CONT |
|
PERCENTILE_DISC |
PERCENTILE_DISC |
|
PERCENT_RANK |
PERCENT_RANK |
Collation¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
COLLATIONPROPERTY |
*to be defined |
|
TERTIARY_WEIGHTS |
*to be defined |
Configuration¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
@@DBTS |
*to be defined |
|
@@LANGID |
*to be defined |
|
@@LANGUAGE |
*to be defined |
|
@@LOCK_TIMEOUT |
*to be defined |
|
@@MAX_CONNECTIONS |
*to be defined |
|
@@MAX_PRECISION |
*to be defined |
|
@@NESTLEVEL |
*to be defined |
|
@@OPTIONS |
*to be defined |
|
@@REMSERVER |
*to be defined |
|
@@SERVERNAME |
CONCAT(’app.snowflake.com’, CURRENT_ACCOUNT( )) |
|
@@SERVICENAME |
*to be defined |
|
@@SPID |
*to be defined |
|
@@TEXTSIZE |
*to be defined |
|
@@VERSION |
*to be defined |
Can be mimicked by using CURRENT_VERSION |
Conversion¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
CAST |
CAST |
Returns NULL if the value isn’t a number, otherwise returns the numeric value as its. |
CONVERT |
Check CONVERT |
Same behavior as CAST |
PARSE |
*to be defined |
|
TRY_CAST |
TRY_CAST |
Returns NULL if the value isn’t a number, otherwise returns the numeric value as its. |
*to be defined |
Same behavior as TRY_CAST |
|
TRY_PARSE |
TRY_CAST |
Behavior may be different when parsing an integer as date or timestamp. |
Cryptographic¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
ASYMKEY_ID |
*to be defined |
|
ASYMKEYPROPERTY |
*to be defined |
|
CERTENCODED |
*to be defined |
|
CERTPRIVATEKEY |
*to be defined |
|
DECRYPTBYASYMKEY |
*to be defined |
|
DECRYPTBYCERT |
*to be defined |
|
DECRYPTBYKEY |
*to be defined |
|
DECRYPTBYKEYAUTOASYMKEY |
*to be defined |
|
DECRYPTBYKEYAUTOCERT |
*to be defined |
|
DECRYPTBYPASSPHRASE |
_*to be defined_ |
Can be mimicked by using DENCRYPT_RAW |
ENCRYPTBYASYMKEY |
*to be defined |
|
ENCRYPTBYCERT |
*to be defined |
|
ENCRYPTBYKEY |
*to be defined |
|
ENCRYPTBYPASSPHRASE |
*to be defined |
Can be mimicked by using ENCRYPT_RAW |
HASHBYTES |
MD5, SHA1, SHA2 |
Currently only supported separated hash. Use proper one according to the required algorithm MD5, is a 32-character hex-encoded SHA1, has a 40-character hex-encoded string containing the 160-bit SHA2, a hex-encoded string containing the N-bit SHA-2 message digest. Sizes are: 224 = SHA-224 256 = SHA-256 (Default) 384 = SHA-384 512 = SHA-512 |
IS_OBJECTSIGNED |
*to be defined |
|
KEY_GUID |
*to be defined |
|
KEY_ID |
*to be defined |
|
KEY_NAME |
*to be defined |
|
SIGNBYASYMKEY |
*to be defined |
|
SIGNBYCERT |
*to be defined |
|
SYMKEYPROPERTY |
*to be defined |
|
VERIFYSIGNEDBYCERT |
*to be defined |
Cursor¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
@@CURSOR_ROWS |
*to be defined |
|
@@FETCH_STATUS |
*to be defined |
|
CURSOR_STATUS |
*to be defined |
Data type¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
DATALENGTH |
OCTET_LENGTH |
Snowflake doesn’t use fractional bytes so length is always calculated as 8 * OCTET_LENGTH |
IDENT_SEED |
*to be defined |
|
IDENT_CURRENT |
*to be defined |
|
IDENTITY |
*to be defined |
|
IDENT_INCR |
*to be defined |
|
SQL_VARIANT_PROPERTY |
*to be defined |
Date & Time¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
@@DATEFIRST |
*to be defined |
|
@@LANGUAGE |
*to be defined |
|
CURRENT_TIMESTAMP |
CURRENT_TIMESTAMP |
|
CURRENT_TIMEZONE |
*to be defined |
|
DATEADD |
DATEADD |
|
DATEDIFF |
DATEDIFF |
|
DATEDIFF_BIG |
*to be defined |
|
DATEFROMPARTS |
DATE_FROM_PARTS |
|
DATENAME |
*to be defined |
This function receives two arguments: a datepart and date. It returns a string. Here are the supported dateparts from TSQL to Snowflake year, yyyy, yy -> DATE_PART(YEAR, “$date”) quarter, qq, q -> DATE_PART(QUARTER, “$date”) weekday, dw -> DAYNAME(“$date”), though only providing an three-letter english day name |
DATEPART |
DATE_PART |
|
DATETIME2FROMPARTS |
*to be defined |
|
DATETIMEFROMPARTS |
*to be defined |
Can be mimicked by using a combination of DATE_FROM_PARTS and TIME_FROM_PARTS |
DATETIMEOFFSETFROMPARTS |
*to be defined |
|
DAY |
DAY |
|
EOMONTH |
*to be defined |
Can be mimicked by using LAST_DAY |
GETDATE |
GETDATE |
|
GETUTCDATE |
*to be defined |
Can be mimicked by using CONVERT_TIMEZONE |
ISDATE |
*to be defined |
Can be mimicked by using TRY_TO_DATE Returns NULL if the value isn’t a date, otherwise returns the date value as its. |
MONTH |
MONTH |
|
SMALLDATETIMEFROMPARTS |
*to be defined |
Can be mimicked by using a combination of DATE_FROM_PARTS and TIME_FROM_PARTS |
SWITCHOFFSET |
*to be defined |
Can be mimicked by using CONVERT_TIMEZONE |
SYSDATETIME |
LOCALTIME |
|
SYSDATETIMEOFFSET |
*to be defined |
Can be mimicked by using CONVERT_TIMEZONE and LOCALTIME |
SYSUTCDATETIME |
*to be defined |
Can be mimicked by using CONVERT_TIMEZONE and LOCALTIME |
TIMEFROMPARTS |
TIME_FROM_PARTS |
|
TODATETIMEOFFSET |
*to be defined |
Can be mimicked by using CONVERT_TIMEZONE |
YEAR |
YEAR |
JSON¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
ISJSON |
CHECK_JSON |
This is a ‘preview feature’ in Snowflake |
JSON_VALUE |
*to be defined |
Can be mimic by using TO_VARCHAR(GET_PATH(PARSE_JSON(JSON), PATH)) |
JSON_QUERY |
*to be defined |
|
JSON_MODIFY |
*to be defined |
Mathematical¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
ABS |
ABS |
|
ACOS |
ACOS |
|
ASIN |
ASIN |
|
ATAN |
ATAN |
|
ATN2 |
ATAN2 |
|
CEILING |
CEIL |
|
COS |
COS |
|
COT |
COT |
|
DEGREES |
DEGREES |
|
EXP |
EXP |
|
FLOOR |
FLOOR |
|
LOG |
LN |
|
LOG10 |
LOG |
|
PI |
PI |
|
POWER |
POWER |
|
RADIANS |
RADIANS |
|
RAND |
RANDOM |
|
ROUND |
ROUND |
|
SIGN |
SIGN |
|
SIN |
SIN |
|
SQRT |
SQRT |
|
SQUARE |
SQUARE |
Logical¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
CHOOSE |
*to be defined |
Can be mimic by using DECODE |
GREATEST |
GREATEST |
|
IIF |
IIF |
|
LEAST |
LEAST |
|
NULLIF |
NULLIF |
Metadata¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
TransactSQL |
Snowflake |
Notes |
@@PROCID |
*to be defined |
|
APP_NAME |
*to be defined |
|
APPLOCK_MODE |
*to be defined |
|
APPLOCK_TEST |
*to be defined |
|
ASSEMBLYPROPERTY |
*to be defined |
|
COL_LENGTH |
A UDF named COL_LENGTH_UDF is provided to retrieve this information. This UDF works only with VARCHAR types, as specified in the Transact-SQL documentation. For other data types, it returns NULL. |
|
COL_NAME |
*to be defined |
|
COLUMNPROPERTY |
*to be defined |
|
DATABASE_PRINCIPAL_ID |
*to be defined |
Maps to CURRENT_USER when no args |
DATABASEPROPERTYEX |
*to be defined |
|
DB_ID |
*to be defined |
We recommend changing to CURRENT_DATABASE(). If there is a need to emulate this functionality. |
DB_NAME |
*to be defined |
Mostly used in the procedurename mentioned above |
FILE_ID |
*to be defined |
|
FILE_IDEX |
*to be defined |
|
FILE_NAME |
*to be defined |
|
FILEGROUP_ID |
*to be defined |
|
FILEGROUP_NAME |
*to be defined |
|
FILEGROUPPROPERTY |
*to be defined |
|
FILEPROPERTY |
*to be defined |
|
FULLTEXTCATALOGPROPERTY |
*to be defined |
|
FULLTEXTSERVICEPROPERTY |
*to be defined |
|
INDEX_COL |
*to be defined |
|
INDEXKEY_PROPERTY |
*to be defined |
|
INDEXPROPERTY |
*to be defined |
|
NEXT VALUE FOR |
*to be defined |
|
OBJECT_DEFINITION |
*to be defined |
|
OBJECT_ID |
*to be defined |
In most cases can be replaced. Most cases are like: IF OBJECT_ID(‘dbo.TABLE’) IS NOT NULL DROP TABLE dbo.Table which can be replaced by a DROP TABLE IF EXISTS (this syntax is also supported in SQL SERVER). If the object_id needs to be replicated, a UDF is added depending on the second parameter of the function call. |
OBJECT_NAME |
*to be defined |
Can be replaced by: CREATE OR REPLACE PROCEDURE FOO() RETURNS STRING LANGUAGE JAVASCRIPT AS ‘ var rs = snowflake.execute({sqlText: |
OBJECT_NAME(@@PROCID) |
‘ObjectName’ |
This transformation only occurs when it is inside a DeclareStatement. |
OBJECT_SCHEMA_NAME |
*to be defined |
|
OBJECT_SCHEMA_NAME(@@PROCID) |
:OBJECT_SCHEMA_NAME |
This transformation only occurs when it is inside a DeclareStatement. |
OBJECTPROPERTY |
*to be defined |
|
OBJECTPROPERTYEX |
*to be defined |
|
ORIGINAL_DB_NAME |
*to be defined |
|
PARSENAME |
PARSENAME_UDF |
It creates a UDF to emulate the same behavior of Parsename function. |
*to be defined |
||
SCHEMA_NAME |
*to be defined |
|
SCOPE_IDENTITY |
*to be defined |
It this is needed I would recommend to use sequences, and capture the value before insert |
SERVERPROPERTY |
*to be defined |
|
STATS_DATE |
*to be defined |
|
TYPE_ID |
*to be defined |
|
TYPE_NAME |
*to be defined |
|
TYPEPROPERTY |
*to be defined |
|
VERSION |
*to be defined |
Ranking¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
DENSE_RANK |
DENSE_RANK |
|
NTILE |
NTILE |
|
RANK |
RANK |
|
ROW_NUMBER |
ROW_NUMBER |
Replication¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
PUBLISHINGSERVERNAME |
*to be defined |
Rowset¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
OPENDATASOURCE |
*to be defined |
|
OPENJSON |
*to be defined |
|
QPENQUERY |
*to be defined |
|
OPENROWSET |
*to be defined |
|
OPENXML |
OPENXML_UDF |
User-defined function used as a equivalent behavior in Snowflake. |
STRING_SPLIT |
SPLIT_TO_TABLE |
The enable_ordinal flag in Transact-SQL’s STRING_SPLIT is not directly supported by Snowflake’s SPLIT_TO_TABLE function. If the ordinal column is required, a user-defined function (UDF) named STRING_SPLIT_UDF will be generated to replicate this behavior. Without the ordinal column, note that STRING_SPLIT returns a single column named value, while SPLIT_TO_TABLE returns three columns: value, index (equivalent to ordinal), and seq. For additional details, see the SPLIT_TO_TABLE documentation. |
Security¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
CERTENCODED |
*to be defined |
|
CERTPRIVATEKEY |
*to be defined |
|
CURRENT_USER |
CURRENT_USER |
|
DATABASE_PRINCIPAL_ID |
*to be defined |
|
HAS_PERMS_BY_NAME |
*to be defined |
|
IS_MEMBER |
*to be defined |
Change to query INFORMATION_SCHEMA although the client might require defining new roles |
IS_ROLEMEMBER |
*to be defined |
Snowflake’s a similar function IS_ROLE_IN_SESSION |
IS_SRVROLEMEMBER |
*to be defined |
|
LOGINPROPERTY |
*to be defined |
|
ORIGINAL_LOGIN |
*to be defined |
|
PERMISSIONS |
*to be defined |
|
PWDCOMPARE |
*to be defined |
|
PWDENCRYPT |
*to be defined |
|
SCHEMA_ID |
*to be defined |
|
SCHEMA_NAME |
*to be defined |
|
SESSION_USER |
*to be defined |
|
SUSER_ID |
*to be defined |
|
SUSER_NAME |
*to be defined |
|
SUSER_SID |
*to be defined |
|
SUSER_SNAME |
*to be defined |
|
sys.fn_builtin_permissions |
*to be defined |
|
sys.fn_get_audit_file |
*to be defined |
|
sys.fn_my_permissions |
*to be defined |
|
SYSTEM_USER |
*to be defined |
|
USER_ID |
*to be defined |
|
USER_NAME |
*to be defined |
Maps to CURRENT_USER |
String¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
ASCII |
ASCII |
|
CHAR |
CHR, CHAR |
|
CHARINDEX |
CHARINDEX |
|
CONCAT |
CONCAT |
|
CONCAT_WS |
CONCAT_WS |
|
COALESCE |
COALESCE |
|
DIFFERENCE |
*to be defined |
|
FORMAT |
TO_CHAR |
The SSC-EWI-0006 or SSC-FDM-0036 could be generated when the format (numeric or date time) is not fully supported. |
LEFT |
LEFT |
|
LEN |
LEN |
|
LOWER |
LOWER |
|
LTRIM |
LTRIM |
|
NCHAR |
*to be defined |
|
PATINDEX |
*to be defined |
Map to REGEXP_INSTR |
QUOTENAME |
QUOTENAME_UDF |
It creates a UDF to emulate the same behavior of Quotename function |
REPLACE |
REPLACE |
|
REPLICATE |
REPEAT |
|
REVERSE |
REVERSE |
|
RIGHT |
RIGHT |
|
RTRIM |
RTRIM |
|
SOUNDEX |
SOUNDEX |
|
SPACE |
*to be defined |
|
STR |
*to be defined |
|
STRING_AGG |
*to be defined |
|
STRING_ESCAPE |
*to be defined |
|
STRING_SPLIT |
SPLIT_TO_TABLE |
|
STUFF |
*to be defined |
CREATE OR REPLACE FUNCTION STUFF(S string, STARTPOS int, LENGTH int, NEWSTRING string) RETURNS string LANGUAGE SQL AS ‘ left(S, STARTPOS) |
SUBSTRING |
SUBSTRING |
|
TRANSLATE |
TRANSLATE |
|
TRIM |
TRIM |
|
UNICODE |
UNICODE |
|
UPPER |
UPPER |
System¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
$PARTITION |
*to be defined |
|
@@ERROR |
*to be defined |
|
@@IDENTITY |
*to be defined |
It this is needed I would recommend to use sequences, and capture the value before insert |
@@PACK_RECEIVED |
*to be defined |
|
@@ROWCOUNT |
*to be defined |
|
@@TRANCOUNT |
*to be defined |
|
BINARY_CHECKSUM |
*to be defined |
|
CHECKSUM |
*to be defined |
|
COMPRESS |
COMPRESS |
Snowflake’s version has a method argument to indicate the compression method. These are the valid values: SNAPPY, ZLIB, ZSTD, BZ2 The compression level is specified in parentheses and must be a non-negative integer |
CONNECTIONPROPERTY |
*to be defined |
|
CONTEXT_INFO |
*to be defined |
|
CURRENT_REQUEST_ID |
*to be defined |
|
CURRENT_TRANSACTION_ID |
*to be defined |
|
DECOMPRESS |
*to be defined |
Snowflake has two functions for these: DECOMPRESS_BINARY and DECOMPRESS_STRING |
ERROR_LINE |
*to be defined |
SnowScript: Not supported in Snowflake with SSC-EWI-0040. JavaScript: Will map to ERROR_LINE helper. EXEC helper will capture the Exception line property from the stack trace. |
ERROR_MESSAGE |
SQLERRM |
Added SSC-FDM-TS0023 returned error message could be different in Snowflake. |
ERROR_NUMBER |
*to be defined |
SnowScript: Not supported in Snowflake with SSC-EWI-0040. JavaScript: Will map to ERROR_NUMBER helper. EXEC helper will capture the Exception code property. |
ERROR_PROCEDURE |
Mapped |
SnowScript: Use current procedure name, added SSC-FDM-TS0023 result value is based on the stored procedure where the function is called instead of where the exception occurs. JavaScript: Will map to ERROR_PROCEDURE helper, taken from the |
ERROR_SEVERITY |
*to be defined |
SnowScript: Not supported in Snowflake with SSC-EWI-0040. |
ERROR_STATE |
SQLSTATE |
SnowScript: Converted to SQLSTATE snowflake property, added SSC-FDM-TS0023 returned value could be different in Snowflake. JavaScript: Helper will capture Exception state property |
FORMATMESSAGE |
FORMATEMESSAGE_UDF |
It creates a UDF to emulate the same behavior of FORMATMESSAGE function but with some limitations. |
GET_FILESTREAM_TRANSACTION_CONTEXT |
*to be defined |
|
GETANSINULL |
*to be defined |
|
HOST_ID |
*to be defined |
|
HOST_NAME |
*to be defined |
|
ISNULL |
NVL |
|
ISNUMERIC |
*to be defined |
No direct equivalent but can be mapped to a custom UDF, returning the same values as in TSQL. |
MIN_ACTIVE_ROWVERSION |
*to be defined |
|
NEWID |
*to be defined |
Maps to UUID_STRING |
NEWSEQUENTIALID |
*to be defined |
|
ROWCOUNT_BIG |
*to be defined |
|
SESSION_CONTEXT |
*to be defined |
|
SESSION_ID |
*to be defined |
|
XACT_STATE |
*to be defined |
|
System Statistical¶
TransactSql |
Snowflake |
Notes |
|---|---|---|
@@CONNECTIONS |
*to be defined |
Snowflake’s a similar function: LOGIN_HISTORY. Returns login events within a specified time range |
@@PACK_RECEIVED |
*to be defined |
|
@@CPU_BUSY |
*to be defined |
|
@@PACK_SENT |
*to be defined |
|
@@TIMETICKS |
*to be defined |
|
@@IDLE |
*to be defined |
|
@@TOTAL_ERRORS |
*to be defined |
|
@@IO_BUSY |
*to be defined |
|
@@TOTAL_READ |
*to be defined |
|
@@PACKET_ERRORS |
*to be defined |
|
@@TOTAL_WRITE |
*to be defined |
Text & Image¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
TEXTPTR |
*to be defined |
|
TEXTVALID |
*to be defined |
Trigger¶
TransactSQL |
Snowflake |
Notes |
|---|---|---|
COLUMNS_UPDATED |
*to be defined |
|
EVENTDATA |
*to be defined |
|
TRIGGER_NESTLEVEL |
*to be defined |
|
UPDATE |
*to be defined |
System functions¶
This section describes the functional equivalents of system functions in Transact-SQL to Snowflake SQL and JavaScript code, oriented to the creation of UDFs in Snowflake.
ISNULL¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Replaces NULL with the specified replacement value. (ISNULL in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
COMPANYNAME |
|---|
SNOWFLAKE |
Snowflake SQL¶
Result:
COMPANYNAME |
|---|
SNOWFLAKE |
NEWID¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Creates a unique value of type uniqueidentifier. (NEWID in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
Warning
Outputs may differ because it generates a unique ID in runtime
SQL Server¶
Result:
ID |
|---|
47549DDF-837D-41D2-A59C-A6BC63DF7910 |
Snowflake SQL¶
Result:
ID |
|---|
6fd4312a-7925-4ad9-85d8-e039efd82089 |
NULLIF¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a null value if the two specified expressions are equal.
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
RESULT1 |
RESULT2 |
|---|---|
6 |
null |
Snowflake SQL¶
Result:
RESULT1 |
RESULT2 |
|---|---|
6 |
null |
@@ROWCOUNT¶
Applies to
SQL Server
Description¶
Returns the number of rows affected by the last statement. (@@ROWCOUNT in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
3 |
Snowflake SQL¶
Result:
:ADDCOUNT |
|---|
3 |
FORMATMESSAGE¶
Applies to
SQL Server
Description¶
Constructs a message from an existing message in sys.messages or from a provided string. (FORMATMESSAGE in Transact-SQL).
Sample Source Pattern¶
Since Snowflake does not support FORMATMESSAGE function, the FORMATMESSAGE_UDF is added to simulate its behavior.
Syntax¶
SQL Server¶
Examples¶
SQL Server¶
Result:
RESULT |
|---|
This is the first variable and this is the second variable. |
Snowflake¶
Result:
RESULT |
|---|
This is the first variable and this is the second variable. |
FORMATMESSAGE_UDF¶
Snowflake does not have a function with the functionality of FORMATMESSAGE. SnowConvert AI generates the following Python UDF to emulate the behavior of FORMATMESSAGE.
This UDF may not work correctly on some cases:
Using the
%I64dplaceholder will throw an error.If the number of substitution arguments is different than the number of place holders, it will throw an error.
Some unsigned placeholders like
%uor%Xwill not behave properly when formatting the value.It cannot handle message_ids.
String functions¶
This section describes the functional equivalents of string functions in Transact-SQL to Snowflake SQL and JavaScript code, oriented to the creation of UDFs in Snowflake.
CHAR¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a single-byte character with the integer sent as a parameter on the ASCII table (CHAR in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Output:
SMALLEST_A |
|---|
ª |
Snowflake SQL¶
Result:
SMALLEST_A |
|---|
ª |
JavaScript¶
Result:
SMALLEST_A |
|---|
ª |
CHARINDEX¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the index of the first occurrence of the specified value sent as a parameter when it matches (CHARINDEX in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Result:
INDEX |
|---|
33 |
Snowflake SQL¶
Result:
INDEX |
|---|
33 |
JavaScript¶
Note
Indexes in Transact start at 1, instead of JavaScript which start at 0.
Result:
INDEX |
|---|
33 |
COALESCE¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Evaluates the arguments in order and returns the current value of the first expression that initially doesn’t evaluate to NULL. For example,SELECT COALESCE(NULL, NULL, ‘third_value’, ‘fourth_value’); returns the third value because the third value is the first value that isn’t null. (COALESCE in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
StartDate |
FIRST_NOT_NULL |
|---|---|
2009-01-14 |
2000-01-01 |
2008-01-31 |
2000-01-01 |
2007-11-11 |
2000-01-01 |
2007-12-05 |
2010-05-30 |
2010-05-31 |
2000-01-01 |
2008-01-06 |
2000-01-01 |
2008-01-24 |
2000-01-01 |
2009-02-08 |
2000-01-01 |
2008-12-29 |
2000-01-01 |
2009-01-16 |
2000-01-01 |
Snowflake SQL¶
Result:
StartDate |
FIRST_NOT_NULL |
|---|---|
2009-01-14 |
2000-01-01 |
2008-01-31 |
2000-01-01 |
2007-11-11 |
2000-01-01 |
2007-12-05 |
2010-05-30 |
2010-05-31 |
2000-01-01 |
2008-01-06 |
2000-01-01 |
2008-01-24 |
2000-01-01 |
2009-02-08 |
2000-01-01 |
2008-12-29 |
2000-01-01 |
2009-01-16 |
2000-01-01 |
CONCAT¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Makes a concatenation of string values with others. (CONCAT in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Output:
TITLE |
|---|
Ray of Light |
Snowflake SQL¶
Output:
TITLE |
|---|
Ray of Light |
JavaScript¶
Output:
————| Ray of Light|
LEFT¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the right part of a character string with the specified number of characters. (RIGHT in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Function used to emulate the behavior
Examples¶
SQL Server¶
Output:
FIRST_NAME |
|---|
John |
Snowflake SQL¶
Output:
FIRST_NAME |
|---|
John |
JavaScript¶
Output:
FIRST_NAME |
|---|
John |
LEN¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the length of a string (LEN in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Output:
LEN |
|---|
11 |
Snowflake SQL¶
Output:
LEN |
|---|
11 |
JavaScript¶
Output:
LEN |
|---|
11 |
LOWER¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Converts a string to lowercase (LOWER in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Output:
LOWERCASE |
|---|
you are a prediction of the good ones |
Snowflake SQL¶
Output:
LOWERCASE |
|---|
you are a prediction of the good ones |
JavaScript¶
Output:
LOWERCASE |
|---|
you are a prediction of the good ones |
NCHAR¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the UNICODE character of an integer sent as a parameter (NCHAR in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Integer expression.
Return Type¶
String value, it depends on the input received.
Examples¶
Query¶
Result¶
ª |
Note
The equivalence for this function in JavaScript is documented in CHAR.
REPLACE¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Replaces all occurrences of a specified string value with another string value. (REPLACE in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Output:
Snowflake SQL¶
Output:
JavaScript¶
Output:
REPLICATE¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Replicates a string value a specified number of times (REPLICATE in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
JavaScript¶
Result:
RIGHT¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the right part of a character string with the specified number of characters. (RIGHT in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
UDF used to emulate the behavior
Examples¶
SQL Server¶
Output:
Snowflake SQL¶
Output:
JavaScript¶
Output:
RTRIM¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a character expression after it removes leading blanks (RTRIM in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Custom function used to emulate the behavior
Examples¶
SQL Server¶
Input:
Output:
Snowflake SQL¶
Result:
JavaScript¶
Result:
SPACE¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a number of occurrences of blank spaces (SPACE in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Custom function used to emulate the behavior
Examples¶
SQL Server¶
Input:
Output:
Snowflake SQL¶
Input:
Output:
JavaScript¶
Input:
Output:
SUBSTRING¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a character expression after it removes leading blanks (RTRIM in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Custom function used to emulate the behavior
Examples¶
SQL Server¶
Input:
Output:
Snowflake SQL¶
Result:
JavaScript¶
Result:
UPPER¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Converts a string to uppercase (UPPER in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Output:
Snowflake SQL¶
Output:
JavaScript¶
Output:
ASCII¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the number code of a character on the ASCII table (ASCII in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: VARCVHAR or CHAR expression.
Return Type¶
INT.
Examples¶
Query¶
Result¶
ASCII in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns the number code of a character on the ASCII table (JavaScript charCodeAt function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
index(Optional): Index of string to get character and return its code number on the ASCII table. If this parameter is not specified, it takes 0 as default. \
Return Type¶
Int.
Examples¶
Query¶
Result¶
QUOTENAME¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a string delimited using quotes (QUOTENAME in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
string_expression: String to delimit.
quote_character: one-character to delimit the string.
Return Type¶
NVARCHAR(258). Null if the quote is different of (‘), ([]), (“), ( () ), ( >< ), ({}) or (`).
Examples¶
Query¶
Result¶
QUOTENAME in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Unfortunately, this function is not available in JavaScript, but it can be implemented using predefined functions.
Sample Source Pattern¶
Implementation Example¶
Arguments¶
string: String expression to delimit.
quote: Quote to be used as a delimiter.
Return Type¶
String.
Examples¶
Query¶
Result¶
CONCAT_WS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Makes a concatenation of string values with others using a separator between them (CONCAT_WS in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
separator: Separator to join.
expression1, ... ,expressionN: Expression to be found into a string.
Return Type¶
String value, depends on the input received.
Examples¶
Query¶
Result¶
Join in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Concatenates the string arguments to the calling string using a separator (JavaScript Join function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
separator: Character to join.
Return Type¶
String.
Examples¶
Query¶
Result¶
SOUNDEX¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a four-character code to evaluate the similarity of two strings (SOUNDEX in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
string_expression: String expression to reverse.
Return Type¶
The same data type of the string expression sent as a parameter.
Examples¶
Query¶
Result¶
SOUNDEX in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Unfortunately, JavaScript does not provide a method that executes the SOUNDEX algorithm, but it can be implemented manually.
Sample Source Pattern¶
Implementation Example¶
Arguments¶
word: String expression to get its SOUNDEX equivalence.
Return Type¶
String.
Examples¶
Query¶
Result¶
REVERSE¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Reverses a string (REVERSE in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
string_expression: String expression to reverse.
Return Type¶
The same data type of the string expression sent as a parameter.
Examples¶
Query¶
Result¶
reverse in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Unfortunately, this function is not available in JavaScript, but it can be implemented using predefined functions.
Sample Source Pattern¶
Implementation Example¶
Arguments¶
string: String expression to reverse.
Return Type¶
String.
Examples¶
Query¶
Result¶
STRING_ESCAPE¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Escapes special characters in texts and returns text with escaped characters. (STRING_ESCAPE in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
text: Text to escape characters.
type: Format type to escape characters. Currently, JSON is the only format supported.
Return Type¶
VARCHAR.
Examples¶
Query¶
Result¶
stringify in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Converts an object to a JSON string format (JavaScript stringify function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
value: Object expression to convert.
Return Type¶
String.
Examples¶
Query¶
Result¶
TRIM¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a character expression without blank spaces (TRIM in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
string_expression: String expressions to convert.
Return Type¶
VARCHAR or NVARCHAR
Examples¶
SQL Server¶
Output:
Snowflake SQL¶
Output:
trim in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Replaces the occurrences of a pattern using a new one sent as a parameter (JavaScript Replace function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
This function does not receive any parameters.
Return Type¶
String.
Examples¶
Query¶
Result¶
DIFFERENCE¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns an integer measuring the difference between two strings using the SOUNDEX algorithm (DIFFERENCE in Transact-SQL).
It counts the common characters of the strings resulting by executing the SOUNDEX algorithm.
Sample Source Pattern¶
Syntax¶
Arguments¶
expression1, expression2: String expressions to be compared.
Return Type¶
Int.
Examples¶
Query¶
Result¶
DIFFERENCE in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Unfortunately, this functionality is not available in JS, but this can be implemented easily.
Note
This functions requires the SOUNDEX algorithm implementation.
Sample Source Pattern¶
Implementation Example¶
Arguments¶
strA, strB: String expressions resulting by executing the SOUNDEX algorithm.
Return Type¶
String.
Examples¶
Query¶
Result¶
FORMAT¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a value formatted with the specified format and optional culture (FORMAT in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
value: String expressions to give format.
format: Desired format.
culture (Optional): NVarchar argument specifying culture. If it is not specified, takes the languages of the current session.
Return Type¶
NULL if the culture parameter is invalid, otherwise, it follows the next data types:
Category |
.NET type |
|
|---|---|---|
Numeric |
bigint |
Int64 |
Numeric |
int |
Int32 |
Numeric |
smallint |
Int16 |
Numeric |
tinyint |
Byte |
Numeric |
decimal |
SqlDecimal |
Numeric |
numeric |
SqlDecimal |
Numeric |
float |
Double |
Numeric |
real |
Single |
Numeric |
smallmoney |
Decimal |
Numeric |
money |
Decimal |
Date and Time |
date |
DateTime |
Date and Time |
time |
TimeSpan |
Date and Time |
datetime |
DateTime |
Date and Time |
smalldatetime |
DateTime |
Date and Time |
datetime2 |
DateTime |
Date and Time |
datetimeoffset |
DateTimeOffset |
Examples¶
Query¶
Result¶
Query¶
Result¶
CURRENCY |
|---|
₡244,900.25 |
FORMAT in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
There are different functions to format date and integer values in JavaScript. Unfortunately, these functionalities are not integrated into one method.
DateTime values¶
Syntax¶
Arguments¶
locales (Optional): String expression of the format to apply.
options (Optional): Object with different supported properties for formats of numeric expressions (JavaScript NumberFormat function Documentation).
value: Numeric expression to format.
Return Type¶
String.
Numeric values¶
Syntax¶
Arguments¶
locales (Optional): String expression of the format to apply.
options (Optional): Object with different supported properties for formats of numeric expressions (JavaScript NumberFormat function Documentation).
value: Numeric expression to format.
Return Type¶
String.
Examples¶
DateTime¶
Query¶
Result¶
Numeric¶
Query¶
Result¶
PATINDEX¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the starting position of the first occurrence of a pattern in a specified expression (PATINDEX in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
pattern: Pattern to find.
expression: Expression to search.
Return Type¶
Integer. Returns 0 if the pattern is not found.
Examples¶
Query¶
Result¶
search in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Finds the index of a pattern using REGEX (JavaScript search function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
regex: Regular expression which matches with the desired pattern.
Return Type¶
Integer. If the pattern does not match with any part of the string, returns -1.
Examples¶
Query¶
Result¶
STR¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns character data converted from numeric data. The character data is right-justified, with a specified length and decimal precision. (STR in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Arguments¶
numeric_expression: Float expression with a decimal point.
length (Optional): Length that the returning expression will have, including point notation, decimal, and float parts.
decimal(Optional): Is the number of places to the right of the decimal point.
Return Type¶
VARCHAR.
Examples¶
SQL Server¶
Input:
Output:
Snowflake SQL¶
Input:
Output:
STR in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Unfortunately, this functionality is not available in JS, but it can be implemented easily using the predefined functions for strings.
Sample Source Pattern¶
Implementation Example¶
Arguments¶
number: Float expression with a decimal point.
max_length: Length that the returning expression will have, including point notation, decimal, and float parts.
float_precision: Is the number of places to the right of the decimal point.
Return Type¶
String.
Examples¶
Query¶
Result¶
LTRIM¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a character expression after it removes leading blanks (LTRIM in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
string_expression: String expressions to convert.
Return Type¶
VARCHAR or NVARCHAR
Examples¶
Query¶
Result¶
LTRIM in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Unfortunately, this function is not available in JavaScript, but it can be implemented using regular expressions.
Sample Source Pattern¶
Implementation Example¶
Arguments¶
string: String expression to remove blank spaces.
Return Type¶
String.
Examples¶
Query¶
Result¶
Ranking functions¶
This section describes the functional equivalents of ranking functions in Transact-SQL to Snowflake SQL and JavaScript code, oriented to their usage in stored procedures in Snowflake.
DENSE_RANK¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
This function returns the rank of each row within a result set partition, with no gaps in the ranking values. The rank of a specific row is one plus the number of distinct rank values that come before that specific row. (DENSE_RANK in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
Related EWIs¶
SSC-EWI-0001: Unrecognized token on the line of the source code.
RANK¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question. (RANK in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
Related EWIs¶
SSC-EWI-0001: Unrecognized token on the line of the source code.
ROW_NUMBER¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Numbers the output of a result set. More specifically, returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition. (ROW_NUMBER in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Output:
Snowflake SQL¶
Output:
Related EWIs¶
SSC-EWI-0001: Unrecognized token on the line of the source code.
Logical functions¶
This section describes the functional equivalents of logical functions in Transact-SQL to Snowflake SQL and JavaScript code, oriented to their usage in stored procedures in Snowflake.
IIF¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns one of two values, depending on whether the Boolean expression evaluates to true or false. (IIF in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
XML Functions¶
This section describes the translation of XML functions in Transact-SQL to Snowflake SQL.
Query¶
Applies to
SQL Server
Warning
This transformation will be delivered in the future
Description¶
Specifies an XQuery against an instance of the xml data type. The result is of xml type. The method returns an instance of untyped XML. (Query() in Transact-SQL)
Sample Source Patterns ¶
The following example details the transformation for .query( )
SQL Server ¶
Input¶
Output¶
Snowflake SQL ¶
Input¶
Output¶
Known Issues¶
No issues were found.
Related EWIs¶
SSC-EWI-0036: Data type converted to another data type.
Value¶
Applies to
SQL Server
Warning
This transformation will be delivered in the future
Description¶
Performs an XQuery against the XML and returns a value of SQL type. This method returns a scalar value. (value() in Transact-SQL).
Sample Source Patterns ¶
The following example details the transformation for .value( )
SQL Server ¶
Input¶
Output¶
Snowflake SQL ¶
Input¶
Output¶
Known Issues¶
No issues were found.
Related EWIs¶
SSC-EWI-0036: Data type converted to another data type.
Aggregate functions¶
This section describes the functional equivalents of aggregate functions in Transact-SQL to Snowflake SQL and JavaScript code, oriented to the creation of UDFs in Snowflake.
COUNT¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
This function returns the number of items found in a group. COUNT operates like the COUNT_BIG function. These functions differ only in the data types of their return values. COUNT always returns an int data type value. COUNT_BIG always returns a bigint data type value. (COUNT in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
TOTAL |
|---|
290 |
Snowflake SQL¶
Result:
TOTAL |
|---|
290 |
COUNT_BIG¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
This function returns the number of items found in a group. COUNT_BIG operates like the COUNT function. These functions differ only in the data types of their return values. COUNT_BIG always returns a bigint data type value. COUNT always returns an int data type value. (COUNT_BIG in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
TOTAL |
|---|
290 |
Snowflake SQL¶
Result:
TOTAL |
|---|
290 |
SUM¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Returns the sum of all the values, or only the DISTINCT values, in the expression. SUM can be used with numeric columns only. Null values are ignored. (SUM in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
TOTALVACATIONHOURS |
|---|
14678 |
Snowflake SQL¶
Result:
TOTALVACATIONHOURS |
|---|
14678 |
SnowConvert AI custom UDFs¶
Description¶
Some Transact-SQL functions or behaviors may not be available or may behave differently in Snowflake. To minimize these differences, some functions are replaced with SnowConvert AI Custom UDFs.
These UDFs are automatically created during migration, in the UDF Helper folder, inside the Output folder. There is one file per custom UDF.
OPENXML UDF¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
This custom UDF is added to process a rowset view over an XML document. This would be used for declarations in because it works as a rowset provider.
Optional parameters and different node types are not supported in this version of the UDF. The element node is processed by default.
Custom UDF overloads¶
Parameters
XML: A
VARCHARthat represents the readable content of the XML.PATH: A varchar that contains the pattern of the nodes to be processed as rows.
UDF¶
Transact-SQL¶
Query¶
Result¶
Snowflake¶
Note
The following example is isolated into a stored procedure because environment variables only support 256 bytes of storage, and the XML demo code uses more than that limit.
Query¶
Result¶
CustomerID |
ContactName |
|---|---|
VINET |
Paul Henriot |
LILAS |
Carlos Gonzlez |
Query¶
Result¶
CustomerID |
ContactName |
|---|---|
VINET |
Paul Henriot |
Known Issues¶
No issues were found.
Related EWIs¶
SSC-EWI-TS0075: Built In Procedure Not Supported.
STR UDF¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This custom UDF converts numeric data to character data.
Custom UDF overloads¶
Parameters¶
FLOAT_EXPR: A numeric expression to be converted to varchar.
FORMAT: A varchar expression with the length and number of decimals of the resulting varchar. This format is automatically generated in SnowConvert.
UDF¶
Transact-SQL¶
Query¶
Result¶
A |
B |
C |
D |
|---|---|---|---|
124 |
** |
123 |
123.5 |
Snowflake¶
Query¶
SWITCHOFFSET_UDF¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This custom UDF is added to return a datetimeoffset value that is changed from the stored time zone offset to a specified new time zone offset.
Custom UDF overloads¶
Parameters
source_timestamp: A TIMESTAMP_TZ that can be resolved to a datetimeoffset(n) value.
target_tz: A varchar that represents the time zone offset
UDF¶
Transact-SQL¶
Query¶
Result¶
Snowflake¶
Query¶
Result¶
fr_time |
cr_time |
|---|---|
1998-09-20 7:45:50.71345 +02:00 |
1998-09-19 23:45:50.7134500 -06:00 |
Metadata functions¶
This section describes the functional equivalents of metadata functions in Transact-SQL to Snowflake SQL and JavaScript code, oriented to their usage in stored procedures in Snowflake.
DB_NAME¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns the name of a specified database.(DB_NAME in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
RESULT |
|---|
ADVENTUREWORKS2019 |
Snowflake SQL¶
Result:
RESULT |
|---|
ADVENTUREWORKS2019 |
Known issues¶
1. CURRENT_DATABASE function has different behavior in certain cases
DB_NAME function can be invoked with the database_id parameter, which returns the name of the specified database. Without parameters, the function returns the current database name. However, Snowflake does not support this parameter and the CURRENT_DATABASE function will always return the current database name.
Related EWIs¶
SSC-FDM-TS0010: CURRENT_DATABASE function has different behavior in certain cases.
OBJECT_ID¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns the database object identification number of a schema-scoped object.(OBJECT_ID in Transact-SQL).
SQL Server syntax¶
Sample Source Patterns¶
1. Default transformation¶
SQL Server¶
Snowflake SQL¶
2. Unknown database¶
SQL Server¶
Snowflake SQL¶
3. Different object names¶
SQL Server¶
Snowflake SQL¶
Known issues¶
1. OBJECT_ID_UDF function has different behavior in certain cases
OBJECT_ID returns the object identification number but the OBJECT_ID_UDF returns a boolean value, so that they are equivalent only when OBJECT_ID is used with not null condition.
Related EWIs¶
SSC-EWI-0001: Unrecognized token on the line of the source code.
SSC-FDM-0007: Element with missing dependencies
Analytic Functions¶
This section describes the functional equivalents of analytic functions in Transact-SQL to Snowflake SQL and JavaScript code, oriented to the creation of UDFs in Snowflake.
LAG¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Accesses data from a previous row in the same result set without the use of a self-join starting with SQL Server 2012 (11.x). LAG provides access to a row at a given physical offset that comes before the current row. Use this analytic function in a SELECT statement to compare values in the current row with values in a previous row. (COUNT in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
PREVIOUS |
ACTUAL |
|---|---|
NULL |
10 |
10 |
89 |
89 |
10 |
10 |
48 |
48 |
0 |
0 |
95 |
95 |
55 |
55 |
67 |
67 |
84 |
84 |
85 |
Snowflake SQL¶
Result:
PREVIOUS |
ACTUAL |
|---|---|
NULL |
10 |
10 |
89 |
89 |
10 |
10 |
48 |
48 |
0 |
0 |
95 |
95 |
55 |
55 |
67 |
67 |
84 |
84 |
85 |
Data Type functions¶
This section describes the functional equivalents of data type functions in Transact-SQL to Snowflake SQL and JavaScript code.
DATALENGTH¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns the number of bytes used to represent any expression. (DATALENGTH in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
SIZE |
|---|
10 |
Snowflake SQL¶
Result:
SIZE |
|---|
10 |
Mathematical functions¶
This section describes the functional equivalents of mathematical functions in Transact-SQL to Snowflake SQL and JavaScript code, oriented to their usage in stored procedures in Snowflake.
ABS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
A mathematical function that returns the absolute (positive) value of the specified numeric expression. (ABS changes negative values to positive values. ABS has no effect on zero or positive values.) (ABS in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Result:
ABS(-5) |
|---|
5 |
Snowflake SQL¶
Result:
ABS(-5) |
|---|
5 |
JavaScript¶
Result:
COMPUTE_ABS(-5) |
|---|
5 |
AVG¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Note
SnowConvert AI Helpers Code section is omitted.
This function returns the average of the values in a group. It ignores null values. (AVG in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
AVG_VACATIONS |
|---|
50 |
Snowflake SQL¶
Result:
AVG_VACATIONS |
|---|
50 |
CEILING¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
A mathematical function that returns the smallest greater integer greater/equal to the number sent as a parameter (CEILING in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
JavaScript¶
Examples¶
SQL Server¶
Result:
CEILING(642.20) |
|---|
643 |
Snowflake SQL¶
Result:
CEIL(642.20) |
|---|
643 |
JavaScript¶
Result:
FLOOR¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the largest integer less than or equal to the specified numeric expression. (FLOOR in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
POWER¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the value of the specified expression to the specified power. (POWER in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
Related Documentation¶
ROUND¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a numeric value, rounded to the specified length or precision. (ROUND in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
Related Documentation¶
SQRT¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the square root of the specified float value. (SQRT in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
SQUARE¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the square of the specified float value. (SQUARE in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
STDEV¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Returns the statistical standard deviation of all values in the specified expression. (STDEV in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
STDEVP¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Returns the statistical standard deviation for the population for all values in the specified expression. (STDVEP in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
VAR¶
Applies to
SQL Server
Azure Synapse Analytics
Note
Some parts in the output code are omitted for clarity reasons.
Description¶
Returns the statistical variance of all values in the specified expression. (VAR in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
POWER¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the value of the specified expression for a specific power.
(POWER in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
base: Base of number, it must be a float expression.
exp: Power to which raise the base.
Return Type¶
The return type depends on the input expression:
| Input Type | Return Type |
|---|---|
| float, real | float |
| decimal(p, s) | decimal(38, s) |
| int, smallint, tinyint | int |
| bigint | bigint |
| money, smallmoney | money |
| bit, char, nchar, varchar, nvarchar | float |
Examples¶
Query¶
Result¶
POW in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the base of the exponent power.
(JavaScript POW function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
base: Base of number, it must be a float expression.
exp: Power to which raise the base.
Return Type¶
Same data type sent through parameter as a numeric expression.
Examples¶
Query¶
Result¶
ACOS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Function that returns the arccosine in radians of the number sent as a parameter (ACOS in Transact-SQL).
Mathematically, the arccosine is the inverse function of the cosine, resulting in the following definition:
$$y = cos^{-1} \Leftrightarrow x = cos(y)$$
For $$y = cos^{-1}(x)$$:
- Range: $$0\leqslant y \leqslant \pi$$ or $$0^{\circ}\leqslant y \leqslant 180^{\circ}$$
- Domain: $$-1\leqslant x \leqslant 1$$
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric float expression, where expression is in$$[-1,1]$$.
Return Type¶
Numeric float expression between 0 and π. If the numeric expression sent by parameter is out of the domain $$[-1, 1]$$, the database engine throws an error.
Examples¶
Query¶
Result¶
ACOS in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Function that returns the arccosine of a specified number
(JavaScript ACOS function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expression, where expression is in$$[-1,1]$$.
Return Type¶
Numeric expression between 0 and π. If the numeric expression sent by parameter is out of the range of the arccosine in radians $$[-1, 1]$$, the function returns NaN.
Examples¶
Query¶
Result¶
ASIN¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Function that returns the arcsine in radians of the number sent as parameter (ASIN in Transact-SQL).
The arcsine is the inverse function of the sine , summarized in the next definition:
$$y = sin^{-1} \Leftrightarrow x = sin(x)$$
For $$y = sin^{-1}(x)$$:
- Range: $$-\frac{\pi}{2}\leqslant y \leqslant \frac{\pi}{2}$$ or $$-90^{\circ}\leqslant y \leqslant 90^{\circ}$$
- Domain: $$-1\leqslant x \leqslant 1$$
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric float expression, where expression is in$$[-1,1]$$.
Return Type¶
Numeric float expression between $$-\frac{\pi}{2}$$ and $$\frac{\pi}{2}$$. If the numeric expression sent by parameter is not in $$[-1, 1]$$, the database engine throws an error.
Examples¶
Query¶
Result¶
ASIN in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Function that returns the arcsine of a specified number
(JavaScript ASIN function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expression, where expression is in$$[-1,1]$$.
Return Type¶
Numeric expression between $$-\frac{\pi}{2}$$ and $$\frac{\pi}{2}$$. If the numeric expression sent by parameter is out of the domain of the arccosine $$[-1, 1]$$, the function returns NaN.
Examples¶
Query¶
Result¶
COS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Function that returns the cosine of the angle sent through parameters (must be measured in radians) (COS in Transact-SQL).
The cosine is defined as:
$$y = cos(x)$$
Where:
- Range: $$-1\leqslant y \leqslant 1$$
- Domain: $$\mathbb{R}$$
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric float expression, where expression is in $$\mathbb{R}$$.
Return Type¶
Numeric float expression in $$[-1, 1]$$.
Examples¶
Query¶
Result¶
COS in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Static function that returns the cosine of an angle in radians
(JavaScript COS function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expressions.
Return Type¶
Same data type sent through parameter as a numeric expression.
Examples¶
Query¶
Result¶
COT¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the cotangent of the angle in radians sent through parameters (COT in Transact-SQL).
The cosine is defined as:
$$cot(x) = \frac{cos(x)}{sin(x)}$$ or $$cot(x) = \frac{1}{tan(x)}$$
To calculate the cosine, the parameter must comply with the constraints of sine and cosine functions.
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric float expression, where expression is in $$\mathbb{R}-{sin(expression)=0 \wedge tan(expression) =0}$$.
Return Type¶
Numeric float expression in $$\mathbb{R}$$.
Examples¶
Query¶
Result¶
COT in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Unfortunately, the object Mathin JavaScript does not provide a method to calculate the cotangent of a given angle.
This could be calculated using the equation: $$cot(x) = \frac{cos(x)}{sin(x)}$$
Sample Source Pattern¶
Implementation example¶
Arguments¶
angle: Numeric expression in radians.
Return Type¶
Same data type sent through parameter as a numeric expression.
Examples¶
Query¶
Result¶
RADIANS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Converts degrees to radians.
(RADIANS in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expression in degrees.
Return Type¶
Same data type sent through parameter as a numeric expression in radians.
Examples¶
Query¶
Result¶
RADIANS(180) |
|---|
3.141592653589793116 |
Note
Cast the parameter of this function to float, otherwise, the above statement will return 3 instead of PI value.
RADIANS in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
JavaScript does not provide a method to convert degrees to radians of a given angle.
This could be calculated using the equation: $$Radians = \frac{\pi}{180^{\circ}} \cdot angle$$
Sample Source Pattern¶
Implementation example¶
Arguments¶
angle: Float expression in degrees.
Return Type¶
Same data type sent through parameter as a numeric expression in radians.
Examples¶
Query¶
Result¶
RADIANS(180) |
|---|
3.141592654 |
PI¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the constant value of PI
(PI in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
This method does not receive any parameters.
Return Type¶
Float.
Examples¶
Query¶
Result¶
PI in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Constant which represents the PI number (approximately 3.141592…)
(JavaScript PI Documentation).
Sample Source Pattern¶
Syntax¶
Examples¶
Query¶
Result¶
DEGREES¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Converts the angle in radians sent through parameters to degrees (DEGREES in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric float expression in radians.
Return Type¶
Same data type sent through parameter as a numeric expression.
Examples¶
Query¶
Result¶
DEGREES in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
JavaScript does not provide a method to convert radians to degrees of a given angle.
This could be calculated using the equation: $$Degrees = \frac{180^{\circ}}{\pi} \cdot angle$$
Sample Source Pattern¶
Implementation example¶
Arguments¶
angle: Numeric expression in radians.
Return Type¶
Same data type sent through parameter as a numeric expression.
Examples¶
Query¶
Result¶
LOG¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the natural logarithm of a number
(LOG in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expression.
base (optional): Base to calculate the logarithm of a number, it is Euler by default.
Return Type¶
Float.
Examples¶
Query¶
Result¶
LOG in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the logarithm using the Euler’s number as a base. (JavaScript LOG function Documentation).
Warning
Unfortunately, JavaScript does not provide a method that receives a logarithm base through its parameters, but this can be solved by dividing the base by the argument.
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expression. It must be positive, otherwise returns NaN.\
Return Type¶
Same data type sent through parameter as a numeric expression.
Examples¶
Query¶
Result¶
ATAN¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Function that returns the arctangent in radians of the number sent as a parameter (ATAN in Transact-SQL).
The arctangent is the inverse function of the tangent, summarized in the next definition:
$$y = arctan^{-1} \Leftrightarrow x = tan(x)$$
For $$y = tan^{-1}(x)$$:
- Range: $$-\frac{\pi}{2}\leqslant y \leqslant \frac{\pi}{2}$$ or $$-90^{\circ}\leqslant y \leqslant 90^{\circ}$$
- Domain: $$\mathbb{R}$$
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric float expression, or a numeric type which could be converted to float.
Return Type¶
Numeric float expression between $$-\frac{\pi}{2}$$ and $$\frac{\pi}{2}$$.
Examples¶
Query¶
Result¶
ATAN in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Function that returns the arctangent of a specified number
(JavaScript ATAN function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expression.
Return Type¶
Numeric expression between $$-\frac{\pi}{2}$$ and $$\frac{\pi}{2}$$.
Examples¶
Query¶
Result¶
ATN2¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Function that returns the arctangent in radians of two coordinates sent as a parameter (ATN2 in Transact-SQL).
For $$z = tan^{-1}(x, y)$$:
- Range: $$-\pi\leqslant z \leqslant \pi$$ or $$-180^{\circ}\leqslant z \leqslant 180^{\circ}$$
- Domain: $$\mathbb{R}$$
Sample Source Pattern¶
Syntax¶
Arguments¶
expression1and expression2: Numeric expressions.
Return Type¶
Numeric expression between $$-\pi$$ and $$\pi$$.
Examples¶
Query¶
Result¶
ATAN2 in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Function that returns the arctangent of two parameters
(JavaScript ATAN2 function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression_1and expression_2: Numeric expressions.
Return Type¶
Numeric expression between $$-\pi$$ and $$\pi$$.
Examples¶
Query¶
Result¶
LOG10¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the base 10 logarithm of a number
(LOG10 in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expression, must be positive.
Return Type¶
Float.
Examples¶
Query¶
Result¶
LOG10 in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the base 10 logarithm of a number
(JavaScript LOG10 function Documentation).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expression. It must be positive, otherwise returns NaN.\
Return Type¶
Same data type sent through parameter as a numeric expression.
Examples¶
Query¶
Result¶
EXP¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the exponential value of Euler (EXP in Transact-SQL).
Sample Source Pattern¶
Syntax¶
Arguments¶
expression: Numeric expression.
Return Type¶
Same data type sent through parameter as a numeric expression.
Examples¶
Query¶
Result¶
EXP in JS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Constant which represents Euler’s number (approximately 2.718…)
(JavaScript Euler’s Number Documentation).
JavaScript allows make different operations using this constant, instead of Transact-SQL which only supports the exponential of Euler.
Sample Source Pattern¶
Syntax¶
Examples¶
Query¶
Result¶
Conversion functions¶
This section describes the functional equivalents of date & time functions in Transact-SQL to Snowflake SQL code.
CONVERT¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Convert an expression of one data type to another. (CONVERT in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result¶
MyDate |
|---|
1998 |
Snowflake SQL¶
Result¶
MYDATE |
|---|
1998 |
Casting date type to varchar¶
SQL Server¶
Result¶
RESULT |
|---|
12/08/22 |
Swowflake SQL¶
Result¶
RESULT |
|---|
12/08/22 |
Casting date type to varchar with size¶
SQL Server¶
Result¶
RESULT |
|---|
07 |
Snowflake SQL¶
Result¶
RESULT |
|---|
07 |
The supported formats for dates casts are:
Date formats
Code |
Format |
|---|---|
1 |
mm/dd/yy |
2 |
yy.mm.dd |
3 |
dd/mm/yy |
4 |
dd.mm.yy |
5 |
dd-mm-yy |
6 |
dd-Mon-yy |
7 |
Mon dd, yy |
10 |
mm-dd-yy |
11 |
yy/mm/dd |
12 |
yymmdd |
23 |
yyyy-mm-dd |
101 |
mm/dd/yyyy |
102 |
yyyy.mm.dd |
103 |
dd/mm/yyyy |
104 |
dd.mm.yyyy |
105 |
dd-mm-yyyy |
106 |
dd Mon yyyy |
107 |
Mon dd, yyyy |
110 |
mm-dd-yyyy |
111 |
yyyy/mm/dd |
112 |
yyyymmdd |
Time formats
Code |
Format |
|---|---|
8 |
hh:mm:ss |
14 |
hh:mm:ss:ff3 |
24 |
hh:mm:ss |
108 |
hh:mm:ss |
114 |
hh:mm:ss:ff3 |
Date and time formats
0 |
Mon dd yyyy hh:mm AM/PM |
9 |
Mon dd yyyy hh:mm:ss:ff3 AM/PM |
13 |
dd Mon yyyy hh:mm:ss:ff3 AM/PM |
20 |
yyyy-mm-dd hh:mm:ss |
21 |
yyyy-mm-dd hh:mm:ss:ff3 |
22 |
mm/dd/yy hh:mm:ss AM/PM |
25 |
yyyy-mm-dd hh:mm:ss:ff3 |
100 |
Mon dd yyyy hh:mm AM/PM |
109 |
Mon dd yyyy hh:mm:ss:ff3 AM/PM |
113 |
dd Mon yyyy hh:mm:ss:ff3 |
120 |
yyyy-mm-dd hh:mm:ss |
121 |
yyyy-mm-dd hh:mm:ss:ff3 |
126 |
yyyy-mm-dd T hh:mm:ss:ff3 |
127 |
yyyy-mm-dd T hh:mm:ss:ff3 |
Islamic calendar dates
Code |
Format |
|---|---|
130 |
dd mmm yyyy hh:mi:ss:ff3 AM/PM |
131 |
dd mmm yyyy hh:mi:ss:ff3 AM/PM |
If there is no pattern matching with the current code, it will be formatted to yyyy-mm-dd hh:mm:ss
Converting string to DATE or DATETIME with style¶
When CONVERT targets a DATE, DATETIME, or DATETIME2 type and includes a literal style code, SnowConvert AI maps it to TO_DATE or TO_TIMESTAMP with the corresponding Snowflake format string.
SQL Server¶
Snowflake SQL¶
The following table shows which target types produce TO_DATE versus TO_TIMESTAMP:
Target Type |
Snowflake Function |
|---|---|
DATE |
TO_DATE |
DATETIME |
TO_TIMESTAMP |
DATETIME2 |
TO_TIMESTAMP |
Converting VARBINARY / BINARY with style¶
When converting to VARBINARY or BINARY with a hex style (1 or 2), SnowConvert AI maps to TO_BINARY(expr, 'HEX'). Style 0 (default/ASCII) maps to a plain CAST. For VARBINARY(MAX), the outer CAST is omitted.
SQL Server¶
Snowflake SQL¶
Converting with a dynamic style variable¶
When the style argument is a variable or expression instead of a literal, SnowConvert AI cannot determine the format string at conversion time. The function falls back to CAST and emits SSC-EWI-TS0098.
SQL Server¶
Snowflake SQL¶
Related EWIs¶
SSC-EWI-TS0098: CONVERT with a non-literal style cannot be mapped to a Snowflake format string.
TRY_CONVERT¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.
(SQL Server Language Reference TRY_CONVERT)
Syntax¶
Source Patterns¶
Basic Transformation¶
To transform this function, we have to check the parameters of the TRY_CONVERT first.
If the expression that needs to be casted is a string, it will be transfomed to TRY_CAST, which is a function of Snowflake.
TRY_CAST¶
The TRY_CAST shares the same transformation with TRY_CONVERT.
Example¶
Sql Server¶
The result will be the same with the example of TRY_CONVERT.
Snowflake¶
Known Issues¶
If the data type is Varchar or Char, then it will be transformed differently.
If TRY_CAST is used with VARCHAR or CHAR in Snowflake, it will cause an error, so it will be transformed to
The same happens with the data types of SQL_VARIANT and GEOGRAPHY.
Are transformed to
If the expression is not a string, there is a very high chance that it will fail, since the TRY_CAST of snowflake works only with string expressions.
In this case, another transformation will be done
Will be transformed to
Now, with these transformation, there could be problems depending on what is being done with the functions. The TRY_CONVERT of SqlServer returns nulls if the convertion was not possible.
This can be used to do logic like this
That type of conditions with the TRY_CONVERT can be used with the TRY_CAST, but what happens if it is transformed to TO_VARCHAR, TOCHAR or to the CAST? If the convertion in those functions fails, it will cause an error instead of just returning null.
Examples¶
In this sample we have several TRY_CONVERT with different data types
SQL Server¶
If we migrate that select, we will get the following result
Snowflake¶
Let’s execute each one and compare the result.
Alias |
SqlServer Result |
Snowflake Result |
|---|---|---|
NUMERIC_RESULT |
12345 |
12345 |
DECIMAL_RESULT |
123.45 |
123.45 |
INT_RESULT |
123 |
123 |
FLOAT_RESULT |
123.02 |
123.02 |
DOUBLE_PRECISION_RESULT |
123.02 |
123.02 |
DATE_RESULT |
2017-01-01 |
2017-01-01 |
DATETIME_RESULT |
2017-01-01 12:00:00.000 |
2017-01-01 12:00:00.000 |
SMALLDATETIME_RESULT |
2017-01-01 12:00:00 |
2017-01-01 12:00:00.000 |
TIME_RESULT |
12:00:00.0000000 |
12:00:00 |
TIMESTAMP_RESULT |
0x323031372D30312D |
2017-01-01 12:00:00.000 |
DATETIMEOFFSET_RESULT |
2017-01-01 12:00:00.0000000 +00:00 |
2017-01-01 12:00:00.000 -0800 |
VARCHAR_RESULT |
1234 |
1234 |
CHAR_RESULT |
1 |
1 |
SQL_VARIANT_RESULT |
2017-01-01 12:00:00 |
“2017-01-01 12:00:00” |
GEOGRAPHY_RESULT |
0xE610000001148716D9CEF7D34740D7A3703D0A975EC08716D9CEF7D34740CBA145B6F3955EC0 |
{ “coordinates”: [ [ -122.36, 47.656 ], [ -122.343, 47.656 ] ], “type”: “LineString” } |
Related EWIs¶
SSC-FDM-TS0005: TRY_CONVERT/TRY_CAST could not be converted to TRY_CAST.
Date & Time functions¶
This section describes the functional equivalents of date & time functions in Transact-SQL to Snowflake SQL and JavaScript code.
AT TIME ZONE¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Converts an inputdate to the corresponding datetimeoffset value in the target time zone. (AT TIME ZONE in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
SQL Server¶
Result:
Snowflake SQL¶
Result:
Known Issues¶
Snowflake does not support all the time zones that SQL Server does. You can check the supported time zones at this link.
SQL Server¶
Result:
Snowflake SQL¶
Related EWIs¶
SSC-FDM-TS0024: CURRENT_TIMESTAMP in At Time Zone statement may have a different behavior in certain cases.
SSC-EWI-TS0063: Time zone not supported in Snowflake.
DATEADD¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns an integer representing the specified datepart of the specified date. (DATEPART in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
Snowflake SQL¶
Result:
DATEDIFF¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns the count (as a signed integer value) of the specified datepart boundaries crossed between the specified startdate and enddate. (DATEDIFF in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
DIFF |
|---|
1 |
Snowflake SQL¶
Result:
DIFF |
|---|
1 |
DATEFROMPARTS¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns a date value that maps to the specified year, month, and day values.(DATEFROMPARTS in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
RESULT |
|---|
2022-12-12 |
Snowflake SQL¶
Result:
RESULT |
|---|
2022-12-12 |
DATENAME¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns a character string representing the specified datepart of the specified date. (DATENAME in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Note
This transformation uses several functions depending on the inputs
Examples¶
SQL Server¶
Result:
DATE1 |
DATE2 |
DATE3 |
|---|---|---|
May |
3 |
Tuesday |
Snowflake SQL¶
Result:
DATE1 |
DATE2 |
DATE3 |
|---|---|---|
May |
Tue |
Tue |
DATEPART¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns an integer representing the specified datepart of the specified date. (DATEPART in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
YEAR |
|---|
2022 |
Snowflake SQL¶
Result:
YEAR |
|---|
2022 |
DAY¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns an integer that represents the day (day of the month) of the specified date. (DAY in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
DAY |
|---|
10 |
Snowflake SQL¶
Result:
DAY |
|---|
10 |
EOMONTH¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
This function returns the last day of the month containing a specified date, with an optional offset. (EOMONTH in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
RESULT |
|---|
2022-05-31 |
Snowflake SQL¶
Result:
RESULT |
|---|
2022-05-31 |
GETDATE¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns the current database system timestamp as a datetime value without the database time zone offset. (GETDATE in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
DATE |
|---|
2022-05-06 09:54:42.757 |
Snowflake SQL¶
Result:
DATE |
|---|
2022-05-06 08:55:05.422 |
MONTH¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns an integer that represents the month of the specified date. (MONTH in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
MONTH |
|---|
10 |
Snowflake SQL¶
Result:
MONTH |
|---|
10 |
SWITCHOFFSET¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
The SWITCHOFFSET adjusts a given timestamp value to a specific timezone offset. This is done through numerical values. More information can be found at SWITCHOFFSET (Transact-SQL).
Sample Source Pattern¶
Syntax¶
A UDF Helper accomplish functional equivalence, also it shares the same syntax as the SQLServer’s SWITCHOFFSET function.
SQLServer¶
Snowflake SQL¶
Example¶
SQLServer¶
Result:
fr_time |
cr_time |
|---|---|
1998-09-20 7:45:50.71345 +02:00 |
1998-09-19 23:45:50.7134500 -06:00 |
Snowflake SQL¶
Result:
fr_time |
cr_time |
|---|---|
1998-09-20 7:45:50.71345 +02:00 |
1998-09-19 23:45:50.7134500 -06:00 |
SYSDATETIME¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a datetime2(7) value that contains the date and time of the computer on which the instance of SQL Server is running. (SYSDATETIME in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
SYSTEM_DATETIME |
|---|
2022-05-06 12:08:05.501 |
Snowflake SQL¶
Result:
SYSTEM_DATETIME |
|---|
211:09:14 |
SYSUTCDATETIME¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns a datetime2(7) value that contains the date and time of the computer on which the instance of SQL Server is running. (SYSUTCDATETIME in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
SYSTEM_UTC_DATETIME |
|---|
2023-02-02 20:59:28.0926502 |
Snowflake SQL¶
Result:
SYSTEM_UTC_DATETIME |
|---|
2023-02-02 21:02:05.557 |
YEAR¶
Applies to
SQL Server
Azure Synapse Analytics
Description¶
Returns an integer that represents the year of the specified date. (YEAR in Transact-SQL).
Sample Source Pattern¶
Syntax¶
SQL Server¶
Snowflake SQL¶
Examples¶
SQL Server¶
Result:
YEAR |
|---|
2022 |
Snowflake SQL¶
Result:
YEAR |
|---|
2022 |