SFMC SQL Conversion Functions

Break from the limits of source data types. Use the full power of SQL to transform your values.

Salesforce Marketing Cloud SQL implementation does not support user-defined functions. There are, however, multiple built-in functions that are useful on a day-to-day basis when working with queries.

Here, I will cover only selected conversion functions, that I find most useful for Marketing Automation purposes. They will help you change the value types to enable the use of type-based Salesforce Marketing Cloud User Interface elements and type-specific functions.


In Salesforce Marketing Cloud, you can use two universal conversion functions - CAST and CONVERT.


The first one, CAST, is straightforward and available in all SQL dialects:

Find all subscribers that joined after the given date
FROM ENT._Subscribers
WHERE DateJoined > CAST('2020-10-30' AS DATE)

It takes a value and expected datatype with AS operator in between.

However, in Salesforce Marketing Cloud, it is much better to use CONVERT function instead. It covers all features of CAST plus adds quite a lot more. It is also better supported by the Query Studio (for example, you cannot use CAST in a SELECT part of the query there).


The basic form of CONVERT is nearly the same as the CASE with reversed order of arguments:

Find all subscribers that joined after the given date
FROM ENT._Subscribers
WHERE DateJoined > CONVERT(DATE, '2020-10-30')

With both functions you can stack functions within for more complex calculations:

Calculate the end of the trial period and output date only

Up to this point, CAST and CONVERT seem similar in the features.

However, only CONVERT have an additional third optional argument - style. Thanks to it, you can convert even from non-standard (for the server) formats that would result in an error:

Converting dd/MM/yyyy format throws 'Could not convert date and/or time from string data type '
FROM _Subscribers
WHERE DateJoined > CONVERT(DATE, '30/10/2019') /* Error */

With style codes, we can make it work with minimal change:

Converting dd/MM/yyyy format with appropriate style code
FROM _Subscribers
WHERE DateJoined > CONVERT(DATE, '30/10/2019', 103)

It is not possible with CAST.

You Should Know

You can also leverage style codes to format your dates during conversion to string, without the need to use FORMAT function on top.

CONVERT(NVARCHAR, GETDATE(), 101) AS DateFormat1, /* Output: 10/30/2020 */
CONVERT(NVARCHAR, GETDATE(), 102) AS DateFormat2, /* Output: 2020.10.30 */
CONVERT(NVARCHAR, GETDATE(), 107) AS DateFormat3 /* Output: Oct 30, 2020 */

Data Types#

There are over 30 data types available for conversion, but there are only a few that are useful on a day-to-day basis in Salesforce Marketing Cloud:

Data TypeDescriptionComment
CHARFixed-length string
NCHARFixed-length string with Unicode support
VARCHARVariable-length string
NVARCHARVariable-length string with Unicode supportBest option for most SFMC string use cases
DECIMAL/NUMERICDecimal number with big precisionBest option if you need a decimal number
INTInteger numberBest option if your number is not decimal
BIGINTBig integer numberWhen your numer is bigger than 2,147,483,647, you need BIGINT
DATETIMELegacy date and timeMore friendly default formatting
DATETIME2Modern date and timeBest when you need both date and time
DATEOnly dateBest when you need date only
TIMEOnly timeBest when you need time only

When converting values to the selected data type, you can have even more control by passing optional argument right after type to declare the length of the output.

It is especially useful with DECIMAL as it allows you to provide expected precision (number of digits in a number) and scale (number of digits to the right of the decimal point in a number).

For example, you may have a string field containing a product price - '123.99'. If you convert it to DECIMAL without any arguments, it will round to 124. However, you can provide precision and scale to keep the current format:

CONVERT(DECIMAL, '123.99') AS RoundedConversion /* Output: 124 */
CONVERT(DECIMAL(5,2), '123.99') AS FullConversion /* Output: 123.99 */

Be sure to add correct precision. If it is smaller then the values in your source, it will lead to an error. It's better to have too big precision than too small.

As for scale - if your scale is smaller than in your source, the value is rounded.

You can also use a single argument with string data types. In this case, it won't directly limit the number of characters, but rather the number of bytes (which might be equal or not to the number of characters depending on what characters you are using).

CHAR allows for 1-8000 bytes range, whereas NCHAR due to Unicode support allows for only 1-4000. VARCHAR and NVARCHAR support the same ranges respectively but also have an additional possible option - max that you can use if some values might exceed the range.

You Should Know

Always double-check the choice of data type you want to convert to, as it might have a massive impact into outcome.

Different outcomes depending on the output data type
CAST(6.9 AS INT) AS CastToInt, /* Output: 6 */
CAST(6.9 AS DECIMAL) AS CastToNumeric, /* Output: 7 */
CONVERT(INT, 6.9) AS ConvertToInt, /* Output: 6 */
CONVERT(DECIMAL, 6.9) AS ConvertToNumeric /* Output: 7 */

It is happening because conversions from DECIMAL or FLOAT to INT are truncated. The rest is rounded if no specific precision/scale argument is available.

CONVERT Style Codes#

Just as with data types - there are many style codes available. Here I will cover just the most popular ones (if you don't understand some formats codes, check date formats guide):

Date formatStyle code
MMM dd yyyy hh:mmtt100
dd MMM yyyy106
MMM dd, yyyy107
yyyy-MM-dd HH:mm:ss120
MM/dd/yy hh:mm:ss tt22

You can find the full list of style codes on Microsoft .Net Doc Pages.

Last updated on by Mateusz Dąbrowski