From Kx Wiki
Revision as of 11:12, 10 September 2016 by Stephen Taylor (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Prerequisite: The Basics



Each datatype has a number and a letter associated with it. Check out the table of primitive datatypes here. The most useful command for this section is type, which returns the number corresponding to the input’s datatype.

q)type 5

The 7 returned indicates a long, according to the primitive datatypes table, which is the default integer data type (as of version 3.0). There are various other number datatypes - these can be specified using their corresponding letter as follows.

q)type 5i
q)type 1 2 3h
q)type 5f

What about the rest of the output from type? The “h” is there simply because the output from type is itself a short. The sign indicates whether the input is an atom or a list: atoms have a negative type, whereas lists have a positive type.

Strings and Symbols

Text in kdb+/q is stored as either a string or a symbol. A string is a list of characters, defined using a pair of quotation marks ". A symbol is an atomic datatype, defined using a single backtick `

q)type mystring
q)count mystring

q)type mysymbol
q)count mysymbol

q)count mysymbollist

Strings and symbols are stored differently internally. The main difference you will see now is that a symbol atom can contain any number of characters, whereas a string is really a list of chars.

Tip: use symbols for repetitive lists & strings for mostly unique ones.

Temporal Datatypes

Datatypes 12-19 are all temporal: e.g. time, timestamp, date.

q)type 2016.10.22
q)type 2016.10.12 2014.03.30 1999.11.10

As you can see, the syntax is yyyy.mm.dd. Temporal lists are separated by spaces, just like number lists. Have a look at the other temporal datatypes and see if you can create them.

Each of the temporal datatypes is stored as a number internally, making arithmetic very fast and easy.

q)2015.10.28 + 2
q)12:28:30 + 7
q)12:28:30.000 + 7

Notice the behaviour of the different datatypes - 1 unit can be a day, a second, a millisecond…

Casting Between Datatypes

How can you get a string from a symbol? Can you add a date to a datetime? How do you create an integer from a string? All of these operations require you to cast between datatypes. In q, we use the cast operator “$” and the function “string”. Here are some examples:

q)string `johnsmith	/ symbol to string
q)string 28		/ long to string

q)`int$3.14159		/ float to integer
q)`$"hello"		/ string to symbol 
q)`int$"28"		/ string to integer: returns the ASCII code for each character
50 56i

We recommend that you read both reference pages to understand the above examples: cast, string.


For each datatype, there is a null value. This means that “empty/unassigned” data can still have a type. Each null is in the primitive datatypes table, but here are some examples.

q)x: 1 2 3 0N 3 0N 7
q)type x
q)type `
q)type " "

If you’re looking for things you can do with nulls, check out these pages: null, ^, fills


1. Create a short, p. Check that its type is correct. Cast p to each of the following datatypes:

2. Cast a string to a symbol and back again.

3. Can you figure out when the kdb+ epoch is? (e.g. Which date corresponds to 0?) What happens to earlier dates?

4. What is happening here? How would we get the desired outcome of 28i?

5. Copy out the following code:

q)x:2 3 5 2 4 7 2 1i

What datatype is returned? Can you see what is happening?


Personal tools