VS Function in q

Hello,

I am newbie to KDB/q world. I have a  table t where in one of the column col1, I need to strip all values before # sign. Example if certain rows have “126734i#1”, my select query needs to have only 126734i. I am running below query and getting an error  type

select distinct t:string “#” vs col1  from t where date within  (2017.11.01;2017.11.30)

I thought let me cast to string, but I am getting a length error in below query

select distinct t:string “#” vs col1 from td where date within  (2017.11.01;2017.11.30)

 I will really appreciate quick help.

Thanks

Hi, Munish! 

I will assume you have a table like this: 

t:([]a:10?abc;b:10?(“1234#1”;“12332#2”))`

A solution would be to use a lambda function in an update statement: 

update b:{"I"$("#"vs x)0}'[b] from t

If we look at the meta of the table we can see that the b columns is now an int: 

meta t1

c| t f a

- | -----

a| s

b| i

You can also use select instead of the update if you do not want to change your table. Just change UPDATE with SELECT. .i.e: 

select b:{"I"$("#"vs x)0}'[b] from t

The type error can be caused by using vs with a column or a mixed list. .i.e 

q)exec b from t

“12332#2”

“1234#1”

“12332#2”

“1234#1”

“1234#1”

“1234#1”

“1234#1”

“12332#2”

“1234#1”

“12332#2”

q)“#” vs exec b from t

'type

In this situation you need to use an each adverb to apply vs on each element

"#" vs ' exec b from t

  

Regards! 

Alex 

Thanks Alex. This worked for me. I was also able to make it work through 

update col1_t:{first “#” vs x} each col1 from select distinct col1 from t where date within (2017.11.01;2017.11.30)

select distinct t:string “#” vs col1  from t where date within  (2017.11.01;2017.11.30)