© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2mo ago•
2 replies
Mokacchi

Selecting DATE_TRUNC returns string but types as Date

node-postgresdrizzle ormPostgreSQL
Hi,
I have an issue that drizzle types the return type of
due
due
as Date, even though they are strings.

I boiled it down to the DATE_TRUNC function (and grouping by it) to cause the database to return strings instead,
if I just select and group by
cardSchedules.due
cardSchedules.due
I actually get Date objects back.

Since DATE_TRUNC is supposed to return TIMESTAMP, I'm confused as to why drizzle still gives me strings.
Is there a way how I can select and group by the "hour" of a timestamp but still full Date objects back?


The code:
return db
    .select({
        count: count(),
        due: sql<Date>`DATE_TRUNC('hour', ${cardSchedules.due})`,
    })
    .from(cards)
    .innerJoin(cardSchedules, eq(cardSchedules.cardId, cards.id))
    .innerJoin(decks, eq(cards.deckId, decks.id))
    .where(
        and(
            eq(cards.userId, userId),
            eq(decks.enabledForReview, true),
            gte(cardSchedules.due, startHour),
            lt(cardSchedules.due, endHour),
        ),
    )
    .groupBy(sql`DATE_TRUNC('hour', ${cardSchedules.due})`)
    .orderBy(sql`DATE_TRUNC('hour', ${cardSchedules.due})`);
return db
    .select({
        count: count(),
        due: sql<Date>`DATE_TRUNC('hour', ${cardSchedules.due})`,
    })
    .from(cards)
    .innerJoin(cardSchedules, eq(cardSchedules.cardId, cards.id))
    .innerJoin(decks, eq(cards.deckId, decks.id))
    .where(
        and(
            eq(cards.userId, userId),
            eq(decks.enabledForReview, true),
            gte(cardSchedules.due, startHour),
            lt(cardSchedules.due, endHour),
        ),
    )
    .groupBy(sql`DATE_TRUNC('hour', ${cardSchedules.due})`)
    .orderBy(sql`DATE_TRUNC('hour', ${cardSchedules.due})`);


schema:
due: timestamp("due", { withTimezone: true, mode: "date" }).notNull(),
due: timestamp("due", { withTimezone: true, mode: "date" }).notNull(),
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Timestamp from Postgres returns date object despite schema using string mode?
Drizzle TeamDTDrizzle Team / help
3y ago
TIMESTAMPTZ is returned as string instead of Date object
Drizzle TeamDTDrizzle Team / help
3y ago
date column not being returned as string by drizzle
Drizzle TeamDTDrizzle Team / help
3y ago
sql template string always returns string value
Drizzle TeamDTDrizzle Team / help
3y ago