question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WHERE, Date, and BETWEEN

See original GitHub issue

It appears that there is a bug with how QueryBuilder is handling dates. I only tested with SQLite, but it is possible that this is happening with other implementations.

  const start = new Date(date);
  start.setHours(0, 0, 0, 0);
  const end = new Date(start);
  end.setDate(start.getDate() + 1);
  const result = await model.createQueryBuilder('q')
    // This doesn't work:
    // .where('q.createdAt BETWEEN :start AND :end', { start, end })
    // This does:
    .where(`q.createdAt BETWEEN '${start.toISOString()}' AND '${end.toISOString()}'`)
    .getCount();
  console.log(result);
  return result;

Query in first case:

SELECT COUNT(DISTINCT("q"."id")) as "cnt" FROM "page" "q" WHERE "q"."createdAt" BETWEEN $1 AND $2 
[ 2017-11-16T22:00:00.000Z, 2017-11-17T22:00:00.000Z ]

Query in second case:

SELECT COUNT(DISTINCT("q"."id")) as "cnt" FROM "group" "q" WHERE "q"."createdAt" BETWEEN '2017-11-16T22:00:00.000Z' AND '2017-11-17T22:00:00.000Z'

Versions tested: 0.1.5, 0.1.6

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:16
  • Comments:18 (5 by maintainers)

github_iconTop GitHub Comments

102reactions
pie6kcommented, Apr 16, 2019
import { Between } from 'typeorm';
import { addYears, subYears } from 'date-fns';

// TypeORM Query Operators
export const AfterDate = (date: Date) => Between(date, addYears(date, 100));
export const BeforeDate = (date: Date) => Between(subYears(date, 100), date);

// leter
    return Event.find({
      where: {
        date: AfterDate(new Date()),
      },
    });
33reactions
umutyerebakmazcommented, Apr 27, 2021
if (filter.startDate && filter.endDate) {
    query.andWhere('"createdAt" BETWEEN :startDate AND :endDate', { startDate: filter.startDate, endDate: filter.endDate });
}

I can add a conditional sentence to the query builder and I use it like this, maybe it will be a useful example for others.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL BETWEEN Operator - W3Schools
The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin...
Read more >
SQL query to select dates between two dates - Stack Overflow
By doing this, you can get all the dates between the given intervals. Save this answer.
Read more >
How to Select Data Between Two Dates and Times in SQL ...
SELECT * FROM TABLE_NAME WHERE DATE_TIME_COLUMN BETWEEN 'STARTING_DATE_TIME' AND 'ENDING_DATE_TIME';. Step 1: Create a Database. For this use ...
Read more >
SQL Between, MySQL Between Dates, Not Between
We can use multiple between operators too. Its syntax is: SELECT Column(s) FROM table_name WHERE column_name BETWEEN value1 AND value2 AND ...
Read more >
Selecting records between two date range query - Plus2net
We can collect records between two date fields of a table by using BETWEEN query. We can use this to get records between...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found