sql-servernestjstypeormsql-likeqsqlquery

How to find entire string using typeORM, Nestjs, SQL Server, just like LIKE clause in mysql


I have database called Beta and table called Keys. In that table I have a column licenseKey: string. I want to find the entire licenseKey from the portion of licenseKey.

Just like SQL below query:

SELECT * 
FROM KEYS 
WHERE licenseKey LIKE "%XYZ%"

I have following code in my controller file:

        @Get('getByLicenseKey/:licenseKey')
   async getByLicenseKey(@Param('licenseKey') licenseKey: string) {
        console.log('dsaadfad');
        return this.licenseKeyService.getKeyByLicenseKey(licenseKey);
    }

Service file

async getKeyByLicenseKey(licenseKey: string): Promise<keys[]> {
        return await this.keyRepo.find({ licenseKey: Like(`${licenseKey}`) });
}

Solution

  • This works for me.

    async getKeyByLicenseKey(licenseKey: string): Promise<keys[]> {
        return this.keyRepo.find({
            where:{licenseKey: Like(`%${licenseKey}%`)}
        })
    }