How do I apply substring function for each entry in a query in KDB/Q?
> s:"hello"> s[0 1]> "he"> # apply this to string column 'col' in following query> select col from tab
How do I apply substring function for each entry in a query in KDB/Q?
> s:"hello"> s[0 1]> "he"> # apply this to string column 'col' in following query> select col from tab
If you have a string column, you have a nested list - to index into nested lists see http://code.kx.com/wiki/JB:QforMortals2/lists#Elided\_Indices
Example:
q)show tab:( col:(0N 5)#15?.Q.a)
col
-------
“mikbj”
“lfuwg”
“byfen”
q)select col[;0 1] from tab
x
----
“mi”
“lf”
“by”
Note, if you only want the first N chars (or last -N chars) can use # (take) with adverb:
q)select 2#'col from tab
col
----
“mi”
“lf”
“by”
Mohammad Noor
Wonderful. Thanks :)
Don’t forget about sublist…http://code.kx.com/wiki/Reference/sublist
select 2 sublist’col from tab
Cheers,
Sean