Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get substring into a new column

I have a table that contains a column that has data in the following format - lets call the column "title" and the table "s"

title

ab.123
ab.321 
cde.456
cde.654
fghi.789
fghi.987

I am trying to get a unique list of the characters that come before the "." so that i end up with this:

ab
cde
fghi

I have tried selecting the initial column into a table then trying to do an update to create a new column that is the position of the dot using "ss".

something like this:

t: select title from s
update thedot: (title ss `.)[0] from t

i was then going to try and do a 3rd column that would be "N" number of characters from "title" where N is the value stored in "thedot" column.

All i get when i try the update is a "type" error.

Any ideas? I am very new to kdb so no doubt doing something simple in a very silly way.

like image 298
spinon42 Avatar asked Feb 18 '26 03:02

spinon42


1 Answers

the reason why you get the type error is because ss only works on string type, not symbol. Plus ss is not vector based function so you need to combine it with each '.

q)update thedot:string[title] ss' "." from t
title    thedot
---------------
ab.123   2
ab.321   2
cde.456  3
cde.654  3
fghi.789 4

There are a few ways to solve your problem:

q)select distinct(`$"." vs' string title)[;0] from t
x
----
ab
cde
fghi
q)select distinct(` vs' title)[;0] from t
x
----
ab
cde
fghi

You can read here for more info: http://code.kx.com/q/ref/casting/#vs

like image 179
WooiKent Lee Avatar answered Feb 20 '26 05:02

WooiKent Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!