Date Functions

toStartOfMonth

Truncate a DateTime to the first day of the month for MoM reporting.

Signature

toStartOfMonth(datetime: DateTime): Date

Returns

Date

What it does

Rounds a DateTime or Date to the first calendar day of its month. Returns a Date. Used for month-over-month comparisons and monthly cohort analysis.

toStartOfMonth is the go-to function for monthly time-series aggregations: MAU, MoM revenue growth, monthly retention. It normalises any date within a month to the 1st, so GROUP BY toStartOfMonth(ts) produces exactly one row per calendar month.

Notes

  • Returns a Date (year-month-01), not a DateTime.
  • For fiscal months that don't align with calendar months, use toStartOfInterval with a INTERVAL expression.
  • toRelativeMonthNum is useful when you need an integer month index for array operations.

Example SQL

toStartOfMonth in ClickHouse SQL

toStartOfMonth-example.sql
SELECT
  toStartOfMonth(created_at) AS month,
  count(DISTINCT user_id) AS mau
FROM events
WHERE created_at >= today() - 365
GROUP BY month
ORDER BY month

Common questions

toStartOfMonth questions, answered

What does toStartOfMonth return?

A Date value set to the first day of the month (e.g. 2024-03-01) in the column's time zone.

TypeScript with hypequery

Use toStartOfMonth in a typed TypeScript query

hypequery gives you a type-safe query builder for ClickHouse. The generated schema maps your ClickHouse columns to TypeScript types, and raw SQL expressions let you incorporate functions like toStartOfMonth when you need them inside a builder query.

monthly-active-users.ts
import { createQueryBuilder, rawAs, selectExpr } from '@hypequery/clickhouse';
import type { IntrospectedSchema } from './schema';

const db = createQueryBuilder<IntrospectedSchema>({
  host: process.env.CLICKHOUSE_HOST!,
  username: process.env.CLICKHOUSE_USERNAME!,
  password: process.env.CLICKHOUSE_PASSWORD!,
  database: process.env.CLICKHOUSE_DATABASE!,
});

const mau = await db
  .table('events')
  .select([
    selectExpr('toStartOfMonth(created_at)', 'month'),
    rawAs('uniq(user_id)', 'mau'),
  ])
  .where('created_at', 'gte', new Date(Date.now() - 365 * 86400 * 1000))
  .groupBy('month')
  .orderBy('month', 'ASC')
  .execute();

Use cases

What teams use toStartOfMonth for

Monthly active users ClickHouse query

GROUP BY toStartOfMonth(event_time), COUNT(DISTINCT user_id) gives you MAU per month. Add a WHERE clause to limit to the last 12 months.

Month-over-month growth ClickHouse

Self-join on toStartOfMonth offset by 1 month, or use lagInFrame in a window function over the monthly aggregation.

Related functions

Functions used alongside toStartOfMonth

Next step

Use toStartOfMonth in a type-safe TypeScript query

hypequery generates TypeScript types from your ClickHouse schema. Use toStartOfMonth alongside the builder, and reach for raw SQL expressions when the function is not exposed as a dedicated helper.

Get early access to hypequery cloud.

Cloud hosts the same model you run in your backend, with a managed API and MCP server, versions, and rollback. We're letting teams in a few at a time. Join the waitlist and we'll reach out when your project can go live.

How do you want to build your analytics layer? (Optional)
What features do you most need? (Optional)

Pick as many as you like.

Only your email is required. All other questions are optional.