Search code examples
kdb

select based on value of char array column - KDB


I have a table built like so:

tab: ([]col1:();col2:())
`tab insert (`testsym; "testchararr")

I now want to select the row where col2 has the value "testchararr". I have tried like so:

select from tab where col2 = "test"

but this always returns 'length error.

How can I query based on the value of a char array? Thanks


Solution

  • Use "like" or an adverb. e.g.

    q)select from tab where col2 like "testchararr"
    col1    col2
    ---------------------
    testsym "testchararr"
    
    q)select from tab where col2~\:"testchararr"
    col1    col2
    ---------------------
    testsym "testchararr"
    
    q)select from tab where col2 like "test"
    col1 col2
    ---------
    
    q)select from tab where col2~\:"test"
    col1 col2
    ---------
    

    I advise checking the speed of each method. For more examples of qsql like in use see: http://www.timestored.com/b/forums/topic/string-functions-like-search-replace-regex/