apply substring function to each entry in kdb query

1,630 views
Skip to first unread message

Sam

unread,
Jun 24, 2014, 7:30:11 PM6/24/14
to personal...@googlegroups.com

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

Mohammad Noor

unread,
Jun 24, 2014, 7:45:03 PM6/24/14
to personal...@googlegroups.com
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

Samit Jain

unread,
Jun 24, 2014, 7:59:52 PM6/24/14
to personal...@googlegroups.com
Wonderful.  Thanks :)


--
You received this message because you are subscribed to a topic in the Google Groups "Kdb+ Personal Developers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/personal-kdbplus/Ogh15SyoPYE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to personal-kdbpl...@googlegroups.com.
To post to this group, send email to personal...@googlegroups.com.
Visit this group at http://groups.google.com/group/personal-kdbplus.
For more options, visit https://groups.google.com/d/optout.

Sean O'Hagan

unread,
Jul 1, 2014, 5:37:31 PM7/1/14
to personal...@googlegroups.com
Don't forget about sublist...http://code.kx.com/wiki/Reference/sublist

select 2 sublist'col from tab

Cheers,
Sean
Reply all
Reply to author
Forward
0 new messages