Home > Back-end >  TypeORM return data from the month of a date
TypeORM return data from the month of a date

Time:09-14

I have an employee table and in it there is a date column. I'm trying to return all employees who have the same month informed of the entry date. With the entry 2010-09-01 I want you to return all the employees you have in the column 2010-09-01 to 2010-09-30

async rangeDate(dateInput: string) {
  return await this.employeeRepository
    .createQueryBuilder('employee')      
    .where('employee.date = :dateInput')
    .setParameters({ dateInput })
    .getMany();
}

CodePudding user response:

You can use EXTRACT function of PostgreSQL. See the doc. Using this, the query can be written in the following way, after getting the month from the dateInput:

async rangeDate(dateInput: string) {
  const month = (new Date(dateInput)).getMonth()   1;
  return await this.employeeRepository
    .createQueryBuilder('employee')      
    .where('EXTRACT(month, employee.date) = :month', { month: month })
    .getMany();
}
  • Related