cast string to date kdb - Kdb+ / qStudio

Home Forums Kdb+ / qStudio cast string to date kdb

Tagged: ,

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #111312

    John Dempster
    Keymaster

    How do I cast a string to a date in kdb?
    e.g. Given the date string column in the table below, how do I convert it to a date column?

    `
    q)table:([] a:1 2 3; b:(“2016-11-01″;”2016-11-02″;”2016-11-03”))
    q)table
    a b
    ————–
    1 “2016-11-01”
    2 “2016-11-02”
    3 “2016-11-03”
    `

    Perform an update casting the column using an upper case letter D-for type like so:

    `q)update “D”$b from table
    a b
    ————
    1 2016.11.01
    2 2016.11.02
    3 2016.11.03
    `

    If the date was in a different format mm-dd vs dd-mm or UK vs US date format you could first rearrange the strings then cast:

    `
    q)update {x[0 1 2 3 4 8 9 7 5 6]} each b from table
    a b
    ————–
    1 “2016-01-11”
    2 “2016-02-11”
    3 “2016-03-11”

    q)update “D”${x[0 1 2 3 4 8 9 7 5 6]} each b from table
    a b
    ————
    1 2016.01.11
    2 2016.02.11
    3 2016.03.11

    q)meta update “D”${x[0 1 2 3 4 8 9 7 5 6]} each b from table
    c| t f a
    -| —–
    a| j
    b| d
    `

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.