Search code examples
dateconditional-statementskdb+q-query

Cond (triad $) not working with a month condition in KDB Q


This basic cond works :

$[8i > 3i ; true;false]

but this does not:

$[mm$.z.d > 3i ; true;false]

I don't understand why, since mm$.z.d (current month) is 8i.


Solution

  • kdb reads right-to-left, thus it's comparing

    .z.d>3i 
    

    before it ever gets to the month casting. You need to use parentheses to force the casting first

    q)$[(`mm$.z.d) > 3i ;`true;`false]
    `true
    

    or better yet, refactor your statement to allow for the right-to-left

    q)$[3i<`mm$.z.d;`true;`false]
    `true