Search code examples
stringnulltabskdb+

How to replace a string in a table with a null value using kdb


I have 1 table in the form as follows:

enter image description here

I want to set c2 to "" when c1>2, expect the result to be like this:

enter image description here

I try to do:

t:([] c1:1 2 3 4;c2:("abcd";"efgh";"igkl";"mnop"))
c:enlist (>; `c1; 2)
b:0b;
a:(enlist `c2)!enlist "";
![t;c;b;a]

But it reported an error: length: Incompatible list lengths Please tell me what to do,thanks.


Solution

  • Since c2 is a list of chars (IE a string) you need to enlist "" first & then take (#) the number of strings you need. In this case I've used count[i].

    q)update c2:count[i]#enlist"" from t where c1>2
    c1 c2
    ---------
    1  "abcd"
    2  "efgh"
    3  ""
    4  ""
    

    In functional form:

    q)parse"update c2:count[i]#enlist\"\"from t where c1>2"
    !
    `t
    ,,(>;`c1;2)
    0b
    (,`c2)!,(#;(#:;`i);(enlist;""))
    q)![t;enlist(>;`c1;2);0b;enlist[`c2]!enlist(#;(count;`i);(enlist;""))]
    c1 c2
    ---------
    1  "abcd"
    2  "efgh"
    3  ""
    4  ""