1

I need to perform a global update on a KDB table to update two columns. For the FirstName column, I want to remove it's value for records which have empty string in the SecondName column, and for the FullName column I want to replace an encoded delimiter with a space for all rows in the table.

These need not be done in a single update statement if that helps.

update
    FirstName:$[SecondName like ""; FirstName; ""],
    FullName[FullName; " "; " "]
    from table
    }

I'm struggling with the syntax - the above is my best attempt but it doesn't work.

1
  • If it helps, think of references to column names like vectors, not scalars... Commented Jul 23, 2019 at 15:05

4 Answers 4

2

One way to achieve that in a sinlge update statement:

q) update FirstName:?[SecondName like ""; SecondName;FirstName], FullName:ssr[;" "; " "]@'FullName from table
Sign up to request clarification or add additional context in comments.

2 Comments

Blimey - that was quick :) I only refreshed the page 10 minutes ago and didn't refresh again before posting my own solution - yours is definitely nicer than mine though, thanks!
Glad it helped.
2

For your update for the FirstName you need a ? rather than a $ as the Execution control operator. As it does the execution with a list rather than an atom. For the FullName you will need to use ssr, which finds where string has "&nbsp" and replaces it with " "

Which would give the following:

q)tab:([]FirstName:("aa";"cc");SecondName:("";"dd");FullName:("aa ";"cc dd"))
q)update FirstName:?[SecondName like ""; count[FirstName]#enlist""; FirstName],FullName:ssr[; " ";" "]each FullName from tab
FirstName SecondName FullName
-----------------------------
""        ""         "aa "
"cc"      "dd"       "cc dd"

Hope this answers your question.

Regards, Sander

1 Comment

Thanks very much Sander - seems there are many different ways to achieve this result. I have something that works now so won't waste time trying every solution offered but I reallly appreciate your input :)
1

I would recommend to do it in two steps

//create table with mock data
table: ([]FirstName: ("aaa";"ccc"); SecondName: ("bbb";""); FullName: ("aaa bbb";"ccc "));
//step1: set First to "" whenever SecondName is ""
table: update FirstName: (count i)#enlist"" from table where SecondName like "";
//step2: replace spaces in FullName
table: update FullName: ssr[;" ";" "] each FullName from table;

1 Comment

Hah - very similar to mine - amazed at how many responses I got so quickly - thanks a lot :)
1

Got it I think:

table:update FirstName:(count i)#enlist "" from table where SecondName like "";
table:update FullName:{ ssr[x; " "; " "] } each FullName from table where FullName like "* *";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.