Salesforce Marketing Cloud - SQL Features & Tips Part 1

Creating database queries with SQL is an everyday job in Salesforce Marketing Cloud (SFMC). This makes it easy to retrieve, combine or filter data from various data extensions and data views. The use of SQL in SFMC offers powerful options for data processing and analysis, but also has some special features that users should be aware of.

SQL in Salesforce Marketing Cloud — No mistakes, but still wrong results?

In addition technical and functional limitations of SQL in Marketing Cloud It can happen that queries are executed without errors because the syntax is correct, but still do not produce the desired or plausible results. This discrepancy often goes unnoticed and can lead to unexpected results. We would like to present two such “traps” and give tips on how to avoid them.

Case 1: Time zones for SQL queries in Salesforce Marketing Cloud

Dates are often used to narrow down the population of data extensions or data views to a relevant area. A common condition looks something like this:

WHERE
	CONVERT(DATETIME, SubscriptionDate) > DATEADD(HOUR, -2, GETDATE())

But be careful: When comparing two dates, there is something important to consider — the time zone!

Why does my SQL database query return incorrect results despite the correct syntax?

In our example, the “SubscriptionDate” field is queried from a data extension to filter out contacts who have subscribed to a newsletter within the last two hours. The DATEADD (HOUR, -2, GETDATE ()) part uses the GETDATE () function to determine the current date and time. Two hours are then subtracted from this.

We therefore compare a date that is stored in a data extension with a date that was generated by a function. It is important to know here: SQL functions such as GETDATE () and TODAY () always return the result in the “Central America Standard Time” (CAST) time zone. The same applies to dates in data views. This can result in queries comparing two dates that are not in the same time zone.

Resolve time zone differences in SQL queries in Marketing Cloud

First, you need to know in which time zone the “SubscriptionDate” field in the data extension is stored. This may vary depending on where the field comes from. For example, if the field is imported into SFMC via API, it may well be that you have the field in your regional time zone. Let's assume that the “SubscriptionDate” field is saved in your regional time zone, e.g. in “Central European Standard Time” (CEST), as it applies in Germany. Because CAST does not observe a time change, CAST is 8 hours behind CEST in summer time and 7 hours behind CEST during winter time. If you run the above query at 2:30 PM CEST (in summer), you would get all subscribers that have arrived since 4:30 AM CEST (i.e. over the last 10 hours: the desired two-hour span plus an 8-hour time difference). The desired result — the contacts who have subscribed within the last two hours — is therefore not achieved. You will not receive an error message because there is no logical or syntax error.

The solution: using AT TIME ZONE in SQL queries

The AT TIME ZONE function allows you to convert dates from your current time zone to another. The adjustment in our example would then look like this:

CONVERT(DATETIME, SubscriptionDate) > DATEADD(HOUR, -2, GETDATE()) AT TIME ZONE 'Central America Standard Time' AT TIME ZONE 'Central European Standard Time'

“SubscriptionDate” remains unchanged because it is already in the correct time zone. The right side of the equation is adjusted as follows: The first “AT TIME ZONE” declaration determines which time zone the date is currently in (CAST). The second statement specifies which time zone the date should be converted to (CEST). This ensures that the information on both sides of the equation uses the same time zone.

We hope you found this tip for adjusting time zones useful when querying and comparing data. In the next part, we'll take a closer look at the “UNION” SQL function and explain what you should pay particular attention to when using them in the Marketing Cloud.

Share this article now
link
blog

Even more about Salesforce Marketing Cloud

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud - Why Einstein Engagement Scoring alone isn't enough

Learn everything about the benefits and limits of Einstein Engagement Scoring and when customized scoring models are beneficial.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Release Highlights Winter '25

Salesforce has announced its Marketing Cloud release for winter 2025, which brings a number of interesting new features. We provide an overview of the most important highlights.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud - SQL Features & Tips Part 2

Part 2: Using SQL in Salesforce Marketing Cloud offers powerful options for data processing and analysis, but also brings with it some Marketing Cloud specific features that users should know.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Content Builder Templates

Investing in custom templates for Salesforce Marketing Cloud Content Builder is worthwhile. The result is easy-to-use, CI-compliant, consistent emails that are displayed correctly in all common email clients and save time and money in the long term.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Feature Friday: SFMC Companion

Our Salesforce Marketing Cloud experts present a useful add-on for everyday work in Marketing Cloud, which provides greater clarity and greater efficiency.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Release Highlights Winter '23

Salesforce has announced its Marketing Cloud release for winter 2023, which brings a number of interesting new features. We provide an overview of the most important highlights.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud — Marketing Cloud February 2022 Release

On February 19, the Salesforce Marketing Cloud spring 2022 release goes live. We have summarized our release highlights in a concise manner in our article.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud - Datorama Reports vs. Datorama Reports Advanced

Is a paid upgrade to Datorama Reports Advanced worth it? An overview of the most important functionalities.

salesforce-marketing-cloud
All Categories

Salesforce Marketing Cloud Deployments with Package Manager and Deployment Manager

Wie helfen Package Manager und Deployment Manager beim Rollout standardisierter Prozesse in Marketing Cloud?