Search code examples
kdb

kdb+/q table - convert string to number


assume you have a table

tbl:([] id:("123"; ""; "invalid"))

And want to parse this string into a number.

Invalid values - in the example above, both the empty string "" as well as the value "invalid", should be parsed to null (0Nj).

How can you best do it? My initial approach was

select id:.[value;;0Nj] each enlist each id from tbl

But while that will parse the both the "123" as well as "invalid" entries correctly, it will return the unary operator :: instead of null when trying to parse the row with the empty string.

Of course I could do something like

select id:.[value;;0Nj] each enlist each id from update id:string (count id)#`invalid from tbl where id like ""

but this seems kind of.. ugly/inefficient. Is there any better way to do this?

Thanks


Solution

  • Try "J"$ to cast the column

    q)select "J"$id from tbl
    id
    ---
    123
    

    https://code.kx.com/v2/ref/tok/