In Salesforce Marketing Cloud (SFMC), you might face a situation where you need to query records where a specific date field, such as Check_In_Date__c
, is exactly 90 days from today. A common problem arises when using SQL functions like GETDATE()
because it includes both date and time components, which may cause mismatches if the time parts differ.
Here’s a detailed approach to solve this problem:
Correct Query Using CAST
The issue likely lies in the time component included in the GETDATE()
function. To resolve this, you can cast both the date field and the calculated date to exclude the time part. Use the following query:
sqlCopy codeWHERE CAST(o.Check_In_Date__c AS DATE) = CAST(DATEADD(day, 90, GETDATE()) AS DATE)
This query ensures that only the date components are compared, making it accurate for scenarios where time discrepancies might otherwise cause issues.
Alternative Query Using DATEDIFF
If you prefer a different method, you can use the DATEDIFF
function to calculate the difference in days between Check_In_Date__c
and today’s date. The query would look like this:
<code>sqlCopy code<code>WHERE DATEDIFF(DAY, GETDATE(), o.Check_In_Date__c) = 90</code></code>
This query calculates the exact number of days between GETDATE()
and the Check_In_Date__c
field. If the difference is 90, it retrieves the record.
Explanation of Key Functions
GETDATE()
: Returns the current date and time.CAST()
: Converts a value to a specific data type, in this case,DATE
, to strip off the time part.DATEADD()
: Adds a specific number of days to a date.DATEDIFF()
: Calculates the difference between two dates based on the specified unit, here,DAY
.
By using these queries, you can ensure precise filtering of records that are exactly 90 days from today. Choose the approach that best fits your database design and requirements.
Our Salesforce training in Hyderabad offers hands-on experience with real-world projects, expert instructors, and in-depth knowledge of Salesforce CRM. Whether you’re a beginner or looking to upgrade your skills, this course is designed to meet your needs. Enroll now to kickstart your Salesforce career!
The post SFMC SQL Query for 90 Days Filter appeared first on Salesforce Online Training.