K
Kysely•2y ago
ohmi

Using MySQL functions in SELECT statement

Hi! Just trying to migrate over from Knex. Skimmed over the documentation but still unsure of how to replicate this query in Kysely, or might be looking in the wrong place? In Knex, I'd often have to use selectRaw for these SELECT a, b, UPPER(c) FROM x;
3 Replies
ohmi
ohmiOP•2y ago
Figured it has to do with db.fn , but not quite sure how to fit it in to make things happy
await db.selectFrom("x").select(["a", "b", db.fn("UPPER", ["c"]))
await db.selectFrom("x").select(["a", "b", db.fn("UPPER", ["c"]))
Igal (mobile)
Igal (mobile)•2y ago
Hey 👋 Very close! You have to alias it:
await db.selectFrom('x').select((eb) => [
'a',
'b',
eb.fn('upper', ['c']).as('c_but_upper')
]).execute()
await db.selectFrom('x').select((eb) => [
'a',
'b',
eb.fn('upper', ['c']).as('c_but_upper')
]).execute()
ohmi
ohmiOP•2y ago
awesome, thanks!

Did you find this page helpful?