{"success":true,"name":"query-sql-date-ranges-with-half-open-intervals","description":"Writing SQL WHERE clauses that filter by date ranges.","category":"ai-brimind","path":"ai-brimind/query-sql-date-ranges-with-half-open-intervals/SKILL.md","file":"SKILL.md","content":"---\nname: query-sql-date-ranges-with-half-open-intervals\ndescription: \"Writing SQL WHERE clauses that filter by date ranges.\"\n---\n\n# Query SQL Date Ranges With Half-Open Intervals\n\n## When to use\nProject uses SQL, and you are filtering timestamp or date columns by day, month, or a period, and about to use BETWEEN or date equality.\n\n## Example request\nWHERE created_at BETWEEN '2026-07-01' AND '2026-07-31' → the entire last day after midnight is silently excluded; BETWEEN is inclusive and timestamps have hours. WHERE date_col = '2026-07-07' on a timestamp → matches only exact midnight; a day is a range, not a value. Casting the column to date to compare → the index dies; the half-open range (>= start AND < next_start) keeps it alive. Monthly reports each missing a sliver → boundaries double-counted or dropped between periods; half-open intervals tile perfectly with no overlap.\n\n## Instructions\nAct like a developer who knows a 'day' in a timestamp column is 86,400 seconds, not a value. For every date-range filter: write it half-open — column >= period_start AND column < next_period_start — so 'July' is >= '2026-07-01' AND < '2026-08-01'. This form never loses the last day's afternoon, never double-counts boundaries between consecutive periods, and keeps the column bare so indexes stay usable, unlike casting or wrapping it in date functions. Avoid BETWEEN on timestamps entirely; its inclusive upper bound is the classic off-by-one-day bug. Mind the timezone: know which zone defines where the day starts before computing the boundaries. Verify by checking that a row stamped late on the period's final day is included. Only finalize once every range is half-open and you can say which timezone's midnight anchors it.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1137\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}