Raw SQL in select type to number

Hellow, I have the following sql expression in a select statement array:
sql<number>`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`
sql<number>`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`
but the return type of the column in the row is a string. How can I get the type to be a number?
Solution:
The pg driver returns numeric type as a string. Use the pg-types package to configure the type.
Jump to solution
9 Replies
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
bombillazo
bombillazoOP14mo ago
Im using an expression that is repeatedly used across my query
bombillazo
bombillazoOP14mo ago
Im still getting the data backa s string
No description
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
bombillazo
bombillazoOP14mo ago
postgres 0.26.3
.select([
'sub.id',
sql`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`.as(
'precision_score',
),
]);
.select([
'sub.id',
sql`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`.as(
'precision_score',
),
]);
I just want kysely to return that column as a number Im not sure if raw queries will simply be always returned as string I tried:
sql`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`
.$castTo<number>()
.as('precision_score'),
sql`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`.as(
'precision_score1',
),
sql`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`
.$castTo<number>()
.as('precision_score'),
sql`(${sql.raw(precisionScore)} * 100)::NUMERIC(10,2)`.as(
'precision_score1',
),
and both return as string
Solution
koskimas
koskimas14mo ago
The pg driver returns numeric type as a string. Use the pg-types package to configure the type.
koskimas
koskimas14mo ago
GitHub
GitHub - brianc/node-pg-types: Type parsing for node-postgres
Type parsing for node-postgres. Contribute to brianc/node-pg-types development by creating an account on GitHub.
bombillazo
bombillazoOP14mo ago
got it, thanks
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server