KDB concatenating strings inside select/exec statements
Asked Answered
E

3

6

I have a table T with column Sym:`IBM`MSFT`GOOG... Want the easiest way to create new column of the form newColumn: "IBM_Buy","MSFT_Buy","GOOG_Buy",...

The following does NOT seem to do the trick: select ((string Sym),"_Buy") from T

Eulogist answered 3/6, 2014 at 17:18 Comment(1)
,"_Buy" doesn't work because you're working with a vector in the select statement, not an "atom" (although in this case we're working with strings which are not atoms either!). So user1895961 answer is the correct wayRenie
F
9

You need to use each-left (\:). Think of it as concatenating "_Buy" to each item on a list.

select (string[Sym],\:"_Buy") from T
Fuge answered 3/6, 2014 at 20:9 Comment(2)
+ cast back to a symbol with `$Renie
@user2393012 I thought that, but in his example the new col is populated with strings.Fuge
R
1
t:([]sym:`IBM`MSFT`GOOG)

update newsym:(string sym) cross enlist "_Buy" from t

or easy way (Dictionary Format)

t[`newsym] :
(string t[`sym]) cross enlist "_Buy"
Rely answered 4/6, 2014 at 8:45 Comment(0)
P
1

You may use each-both (') function with anonymous function in select statement:

select {x, "_Buy"}'[Sym] from T

Polypary answered 23/8, 2016 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.