{"id":1495,"date":"2026-09-05T11:00:00","date_gmt":"2026-09-05T05:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/"},"modified":"2026-09-05T11:00:00","modified_gmt":"2026-09-05T05:30:00","slug":"soql-date-literals-bind-variables","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/","title":{"rendered":"SOQL Date Literals and Bind Variables in Apex"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SOQL Date Literals are ready-made words, such as TODAY or LAST_N_DAYS:30, that stand for a date range in a query. Bind variables let you insert Apex values, such as a list of Ids or a calculated date, safely into a query. Together, they make date filters short, readable and safe.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you will learn the most useful date literals, how they handle dates and date times, and every common way to use bind variables. It builds on the <a href=\"https:\/\/swapnilzambare.com\/blog\/soql-where-clause-order-by-limit\/\">SOQL WHERE clause<\/a> and <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-built-in-methods-string-date-math\/\">Apex String, Date and Math methods<\/a>. It also uses the <a href=\"https:\/\/swapnilzambare.com\/blog\/soql-aggregate-functions-group-by\/\">SOQL aggregate functions<\/a> from the previous post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are SOQL Date Literals?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL Date Literals are keywords that represent a relative date or range. For example, <code>THIS_MONTH<\/code> means from the first to the last day of the current month. Salesforce works out the real dates when the query runs. So the same query keeps working tomorrow, next month and next year.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a school notice that says &#8220;homework due tomorrow&#8221;. Nobody writes the exact date, yet everyone understands it. Date literals let your queries speak in the same relative way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Bind Variables?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A bind variable is an Apex variable or expression inside a SOQL query, marked with a colon, such as <code>:startDate<\/code>. Salesforce replaces it with the value at run time. Bind variables also protect queries from SOQL injection, because the value is never treated as query text.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why SOQL Date Literals and Bind Variables Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Real business rules are full of dates and lists, so SOQL Date Literals save a lot of code. Here is why they matter at Bright Path Tutors.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the reminder job needs courses that start in the next 7 days.<\/li>\n\n<li>Also, finance wants enrolments created this month.<\/li>\n\n<li>Next, a trigger must query all courses for a set of enrolment Ids.<\/li>\n\n<li>The sales team wants leads created in the last 30 days.<\/li>\n\n<li>Finally, hard-coded dates break the day after you write them.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So date literals and binds keep code correct over time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How SOQL Date Literals and Bind Variables Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">At run time, Salesforce resolves both parts. First, it reads the current date in the user&#8217;s time zone. Next, it turns each date literal into a start and end date. Then it replaces each bind variable with its Apex value. After that, it runs the query with those real values. Finally, the matching records come back to your code.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-1-1024x640.png\" alt=\"SOQL Date Literals diagram showing date resolution, time zone, literals and bind variables\" class=\"wp-image-1493\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-1-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-1-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">How Salesforce resolves date literals and bind variables at run time.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Single-Day Literals<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">YESTERDAY, TODAY and TOMORROW each cover one day. For example, <code>Start_Date__c = TOMORROW<\/code> finds courses that begin tomorrow.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Range Literals<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">THIS_WEEK, THIS_MONTH, THIS_QUARTER and THIS_YEAR cover the whole period. Their LAST_ and NEXT_ versions cover the period before or after.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Rolling Literals with N<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>LAST_N_DAYS:30<\/code> covers the last 30 days up to today, and <code>NEXT_N_DAYS:7<\/code> covers the 7 days starting tomorrow. You can also use N_DAYS_AGO:n, LAST_N_MONTHS:n and similar forms.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Date vs Date Time Fields<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Date fields, such as Start_Date__c, have no time. In contrast, date time fields, such as CreatedDate, include a time and time zone. SOQL Date Literals handle both, using the running user&#8217;s time zone for date time fields.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Bind Variables and Bind Expressions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A bind can be a simple variable, such as <code>:courseIds<\/code>, or an expression, such as <code>:Date.today().addDays(7)<\/code>. Lists and sets work with IN, which is the most common pattern in triggers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SOQL Date Literals at a Glance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below lists the date literals developers use most.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Literal<\/th><th>Meaning<\/th><th>Example filter<\/th><\/tr><\/thead><tbody><tr><td>TODAY<\/td><td>The current day<\/td><td>CreatedDate = TODAY<\/td><\/tr><tr><td>YESTERDAY<\/td><td>The previous day<\/td><td>CreatedDate = YESTERDAY<\/td><\/tr><tr><td>THIS_MONTH<\/td><td>Current calendar month<\/td><td>Start_Date__c = THIS_MONTH<\/td><\/tr><tr><td>LAST_N_DAYS:n<\/td><td>Last n days up to today<\/td><td>CreatedDate = LAST_N_DAYS:30<\/td><\/tr><tr><td>NEXT_N_DAYS:n<\/td><td>Next n days, from tomorrow<\/td><td>Start_Date__c = NEXT_N_DAYS:7<\/td><\/tr><tr><td>THIS_YEAR<\/td><td>Current calendar year<\/td><td>Start_Date__c = THIS_YEAR<\/td><\/tr><tr><td>THIS_FISCAL_QUARTER<\/td><td>Current fiscal quarter<\/td><td>CloseDate = THIS_FISCAL_QUARTER<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Date Literals vs Bind Variables<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The table below compares the two ways of filtering by date.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Date literal<\/th><th>Bind variable<\/th><\/tr><\/thead><tbody><tr><td>Example<\/td><td>NEXT_N_DAYS:7<\/td><td>:Date.today().addDays(7)<\/td><\/tr><tr><td>Value comes from<\/td><td>Salesforce keyword<\/td><td>Apex code<\/td><\/tr><tr><td>Custom calculations<\/td><td>Limited to built-in ranges<\/td><td>Any Apex logic<\/td><\/tr><tr><td>Readability<\/td><td>Very high<\/td><td>High<\/td><\/tr><tr><td>Works in dynamic SOQL<\/td><td>Yes<\/td><td>Yes, with care<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">When to use which is simple. Use a date literal when a built-in range fits. Use a bind variable when the date comes from custom logic or user input.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-2-1024x640.png\" alt=\"SOQL Date Literals diagram comparing date literals and bind variables\" class=\"wp-image-1494\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-2-768x480.png 768w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-2-1536x960.png 1536w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Date literal vs bind variable.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">A Real SOQL Date Literals Example in Apex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s build date filters for the Bright Path Tutors reminder service.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Courses Starting in the Next 7 Days<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Course__c&gt; upcoming = [\n    SELECT Id, Name, Start_Date__c\n    FROM Course__c\n    WHERE Start_Date__c = NEXT_N_DAYS:7\n    ORDER BY Start_Date__c\n];\nSystem.debug('Courses starting this week: ' + upcoming.size());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Among SOQL Date Literals, NEXT_N_DAYS:7 is one of the most used. It covers the seven days starting tomorrow. So the job always finds the right courses, whatever day it runs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Enrolments Created This Month<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Integer newThisMonth = [\n    SELECT COUNT() FROM Enrolment__c\n    WHERE CreatedDate = THIS_MONTH\n];\nSystem.debug('New enrolments this month: ' + newThisMonth);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">CreatedDate is a date time field. These SOQL Date Literals use the user&#8217;s time zone, so a late evening enrolment in India counts on the correct day.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Bind a Set of Ids<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Set&lt;Id&gt; courseIds = new Set&lt;Id&gt;();\nfor (Enrolment__c e : [SELECT Course__c FROM Enrolment__c WHERE CreatedDate = TODAY]) {\n    courseIds.add(e.Course__c);\n}\n\nMap&lt;Id, Course__c&gt; courses = new Map&lt;Id, Course__c&gt;([\n    SELECT Id, Name, Fee__c FROM Course__c WHERE Id IN :courseIds\n]);\nSystem.debug('Courses with new enrolments today: ' + courses.size());<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, we collect course Ids from today&#8217;s enrolments into a set. Then <code>IN :courseIds<\/code> binds the whole set into one query. This pattern avoids queries inside loops, and you will use it in nearly every trigger.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Bind a Calculated Date and a Number<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Date cutoff = Date.today().addDays(-90);\nDecimal minFee = 5000;\nInteger maxRows = 20;\n\nList&lt;Course__c&gt; oldExpensive = [\n    SELECT Id, Name, Start_Date__c, Fee__c\n    FROM Course__c\n    WHERE Start_Date__c &lt; :cutoff AND Fee__c &gt;= :minFee\n    ORDER BY Start_Date__c DESC\n    LIMIT :maxRows\n];<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, three Apex variables flow into the query. The cutoff date comes from date math, the fee from a number and the row count from LIMIT. This is useful when values come from settings or user input.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Read the Output<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example of the debug log for the first three steps.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>USER_DEBUG [7]|DEBUG|Courses starting this week: 2\nUSER_DEBUG [5]|DEBUG|New enrolments this month: 46\nUSER_DEBUG [9]|DEBUG|Courses with new enrolments today: 4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">SOQL Date Literals and Time Zones<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Time zones confuse many developers who use SOQL Date Literals. Date literals on date time fields use the running user&#8217;s time zone. However, a date time written as text, such as 2026-09-05T00:00:00Z, is in GMT. So prefer literals or bind variables over typed date time strings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with SOQL Date Literals<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Putting quotes around date literals, such as &#8216;TODAY&#8217;.<\/li>\n\n<li>Writing LAST_N_DAYS 30 without the colon.<\/li>\n\n<li>Comparing a Date field with a Datetime bind variable without converting it.<\/li>\n\n<li>Typing date time strings without the Z or a time zone offset.<\/li>\n\n<li>Joining Apex values into query strings instead of binding them.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for SOQL Date Literals<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use date literals whenever a built-in range fits the rule.<\/li>\n\n<li>Use bind variables for values from Apex, settings or users.<\/li>\n\n<li>Bind sets and lists with IN to avoid queries inside loops.<\/li>\n\n<li>Remember that date time literals use the user&#8217;s time zone.<\/li>\n\n<li>Name variables clearly, such as cutoffDate, so queries read well.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Write a query that counts enrolments created in the last 7 days for a set of course Ids. Hint: combine <code>CreatedDate = LAST_N_DAYS:7<\/code> with <code>Course__c IN :courseIds<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Most Asked Interview Questions on SOQL Date Literals<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL Date Literals and bind variables appear in many coding rounds. Here are the 20 questions asked most, with short answers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Questions on SOQL Date Literals<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: What is a date literal in SOQL?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is a keyword, such as TODAY or THIS_MONTH, that stands for a relative date or range.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: Do date literals need quotes?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">No. Date literals are keywords, so you write them without quotes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What does LAST_N_DAYS:30 return?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Records from the last 30 days up to and including today.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4: What is the difference between THIS_MONTH and LAST_N_DAYS:30?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">THIS_MONTH covers the calendar month. In contrast, LAST_N_DAYS:30 is a rolling window that moves every day.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5: What is a bind variable?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is an Apex variable or expression in a SOQL query, marked with a colon, such as :courseIds.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6: How do you filter by a set of Ids?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use IN with a bind variable, for example WHERE Id IN :courseIds.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7: Which date format does SOQL use for typed dates?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Dates use YYYY-MM-DD, and date times use YYYY-MM-DDThh:mm:ssZ, without quotes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code and Scenario Questions on SOQL Date Literals<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8: How do you find courses that start in the next 7 days?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, use WHERE Start_Date__c = NEXT_N_DAYS:7.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9: How do you find records created today?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use WHERE CreatedDate = TODAY. Salesforce applies the user&#8217;s time zone automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10: Can you use a bind variable in LIMIT?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. For example, LIMIT :maxRows works in static SOQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q11: Can a bind variable be an expression?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. For example, :Date.today().addDays(7) is valid in static SOQL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q12: How do bind variables help avoid governor limit issues?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They let you pass whole sets of Ids into one query, so you avoid querying inside loops.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q13: Why do bind variables protect against SOQL injection?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Salesforce treats the bound value only as data, never as query text, so attackers cannot change the query.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q14: How do you find opportunities closing this quarter?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use WHERE CloseDate = THIS_QUARTER, or THIS_FISCAL_QUARTER if your org uses a custom fiscal year.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced Questions on SOQL Date Literals<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q15: How do time zones affect date literals?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For date time fields, date literals use the running user&#8217;s time zone. However, typed date time strings with Z are in GMT.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q16: How do you compare a Date field with a Datetime value?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Convert first, for example with myDatetime.date(), then bind the Date value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q17: Can bind variables be used in dynamic SOQL?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, but the variable must be in scope. A safer option is Database.queryWithBinds with a map of bind values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q18: What is N_DAYS_AGO:n?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It matches records on exactly one day, n days before today.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q19: How would you test code that uses TODAY?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create test data relative to Date.today(), so tests pass on any day. Avoid hard-coded dates in tests.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q20: When should you not use date literals?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the range depends on custom rules, such as a course&#8217;s own duration. Then calculate the date in Apex and bind it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.soql_sosl.meta\/soql_sosl\/sforce_api_calls_soql_select_dateformats.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Date Formats and Date Literals (Salesforce Developers)<\/a><\/li>\n\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/atlas.en-us.apexcode.meta\/apexcode\/langCon_apex_SOQL_variables.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Using Apex Variables in SOQL (Apex Developer Guide)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>SOQL Date Literals, such as TODAY and NEXT_N_DAYS:7, describe relative dates.<\/li>\n\n<li>Date time literals use the running user&#8217;s time zone.<\/li>\n\n<li>Bind variables insert Apex values safely with a colon.<\/li>\n\n<li>Bind sets and lists with IN to avoid queries in loops.<\/li>\n\n<li>Prefer literals and binds over typed date strings.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SOQL searches one object at a time. Next, learn to search many objects at once in our guide to <a href=\"https:\/\/swapnilzambare.com\/blog\/sosl-in-salesforce-search-apex\/\">SOSL in Salesforce<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SOQL Date Literals are ready-made words, such as TODAY or LAST_N_DAYS:30, that stand for a date range in a query. Bind variables let you&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1492,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[8,96],"tags":[98,26,91,7,97,105],"swapz_series":[],"class_list":["post-1495","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","category-salesforce-developer","tag-apex","tag-intermediate","tag-interview-questions","tag-salesforce","tag-salesforce-developer","tag-soql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SOQL Date Literals and Bind Variables in Apex<\/title>\n<meta name=\"description\" content=\"SOQL Date Literals and bind variables explained simply: TODAY, LAST_N_DAYS, THIS_MONTH, date formats, time zones, Apex binds and 20 interview Q&amp;A.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SOQL Date Literals and Bind Variables in Apex\" \/>\n<meta property=\"og:description\" content=\"SOQL Date Literals and bind variables explained simply: TODAY, LAST_N_DAYS, THIS_MONTH, date formats, time zones, Apex binds and 20 interview Q&amp;A.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-05T05:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\"},\"headline\":\"SOQL Date Literals and Bind Variables in Apex\",\"datePublished\":\"2026-09-05T05:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/\"},\"wordCount\":1760,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-date-literals-bind-variables-featured.png\",\"keywords\":[\"Apex\",\"Intermediate\",\"Interview Questions\",\"Salesforce\",\"Salesforce Developer\",\"SOQL\"],\"articleSection\":[\"Salesforce\",\"Salesforce Developer\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/\",\"name\":\"SOQL Date Literals and Bind Variables in Apex\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-date-literals-bind-variables-featured.png\",\"datePublished\":\"2026-09-05T05:30:00+00:00\",\"description\":\"SOQL Date Literals and bind variables explained simply: TODAY, LAST_N_DAYS, THIS_MONTH, date formats, time zones, Apex binds and 20 interview Q&A.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-date-literals-bind-variables-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/soql-date-literals-bind-variables-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"SOQL Date Literals featured image: title card with an Apex code example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/soql-date-literals-bind-variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SOQL Date Literals and Bind Variables in Apex\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/922ac0131aaa7088c78c2cb1700e0c9a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SOQL Date Literals and Bind Variables in Apex","description":"SOQL Date Literals and bind variables explained simply: TODAY, LAST_N_DAYS, THIS_MONTH, date formats, time zones, Apex binds and 20 interview Q&A.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/","og_locale":"en_US","og_type":"article","og_title":"SOQL Date Literals and Bind Variables in Apex","og_description":"SOQL Date Literals and bind variables explained simply: TODAY, LAST_N_DAYS, THIS_MONTH, date formats, time zones, Apex binds and 20 interview Q&A.","og_url":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-09-05T05:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a"},"headline":"SOQL Date Literals and Bind Variables in Apex","datePublished":"2026-09-05T05:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/"},"wordCount":1760,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-featured.png","keywords":["Apex","Intermediate","Interview Questions","Salesforce","Salesforce Developer","SOQL"],"articleSection":["Salesforce","Salesforce Developer"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/","url":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/","name":"SOQL Date Literals and Bind Variables in Apex","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-featured.png","datePublished":"2026-09-05T05:30:00+00:00","description":"SOQL Date Literals and bind variables explained simply: TODAY, LAST_N_DAYS, THIS_MONTH, date formats, time zones, Apex binds and 20 interview Q&A.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/soql-date-literals-bind-variables-featured.png","width":2400,"height":1260,"caption":"SOQL Date Literals featured image: title card with an Apex code example"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/soql-date-literals-bind-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SOQL Date Literals and Bind Variables in Apex"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/922ac0131aaa7088c78c2cb1700e0c9a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=1495"}],"version-history":[{"count":0,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/1495\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/1492"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=1495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=1495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=1495"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=1495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}