Search code examples
kdbq-lang

Remove part of string KDB


I have a table named "t", with column, "x". It looks as follows:

x
------
1.Fred
2.Joe
3.Hank
.
.
.
500.Mary

I need to take the column and remove everything before the name, for example the "1." before Fred.

I have tried update $2_'string x from t but that only works for numbers 1-9 not once I get to the larger numbers. Is there a way to remove the "." and everything before it?


Solution

  • There is one more approach, which helps to cover case when name has '.':

    update x:`${(1+x ?\:".")_'x} string x from  t
    

    The logic is

    1. Convert symbol column x to string
    2. Get index of first dot in every element of list of strings 1+x ?\:"."
    3. Cut characters before the first dot (1+x ?\:".")_'x

    Next model can be used for testing:

    //Create table with 100 random names
    t: ([] ID: til 100; x: `$(string 1+til 100),'".",'(?[;"abc"] each 1+100?9),'" ",'(?[;"def"] each 1+100?9));
    //Replace space with dot for the last 10 names
    t: update x: `$ssr[;" ";"."]'[string x] from t where ID>90;
    update x:`${(1+x ?\:".")_'x} string x from  t
    

    Though using 0: proposed by @Ryan McCarron is obviously faster. If you sure there are no additional dots, that approach is preferable.