Kdb+ provides a large number of data types, in this tutorial we will consider defining the various types. This tutorial forms part of the online kdb+ training course

kdb+ Data Types

For reference the data types available in kdb+ together with their space usage etc. are shown below.
You may also want to download and print our kdb+ cheat sheet that shows the data types and other useful snippets.

type size char num notation Null Value Positive Infinity
Mixed List0
boolean1b11b0b
byte1x40x260x00
short2h542h0Nh0Wh
int4i6420N0W
long8j742j0Nj0Wj
real4e84.2e0Ne0We
float8f94.20n0w
char1c10"z"" "
symbols11`zaphod`
timestamp8p122011.07.08D21:48:48.7031250000Np0Wp
month4m132006.07m0Nm0Wm
date4d142006.07.210Nd0Wd
datetime4z152006.07.21T09:13:390Nz0Wz
timespan8n160D21:56:26.4218750000Nn0Wn
minute4u1712:110Nu0Wu
second4v1812:11:170Nv0Wv
time4t1909:01:02:0420Nt0Wt
enum4*20-77`u$v
table98([] c1:ab`c; c2:10 20 30)
dictionary99`a`b`c!!10 20 30

Defining Data Types

Defining Bytes / Booleans

The "char" for the given type is placed after the value to force it to be that type. Then when we call the type function it returns the type "num" for its argument. In the code above you can see booleans are type num 1 and bytes are type nums 4, this corresponds with what is shown in the data type table above.

Defining Numeric Types

Similar to before, write the "char" at end to force type. Notice that when you do not explicitly specify a type long/float is assumed depending on if your number had decimal places. This behaviour depends on your kdb version, older 2.x defaulted to integers instead of longs.

For floating point numbers, the IEEE standard is used for reals (4 bytes) and floats (8 bytes). Therefore the standard accuracy you would expect is stored. The accuracy shown in the console itself depends on the system P setting.

Defining Character Types

Character data can be stored as char or symbols. Char stores unicode-8/ascii values. Symbols store a sequence of characters as a single entity.

Date Time kdb+ Data Types

Define DateTime TimeStamps etc.

Dot Notation for Accessing Datetime Components or Casting

Using the shorter dot notations dt.dd, dt.mm, dt.ss returns that component

Using the longer dot notations dt.minute, dt.second casts the value to that type, truncating it's value.

Getting the Current Date, Time, TimeStamp

To access the current date, time, timestamp etc. type '.z.' followed by the temporal type's char in the table above; for example .z.t will give the current time, .z.d the current date and so forth.

0N Nulls and 0W Infinities

  • kdb+ provides values for most types to repsent Positive Infinity and Negative Infinity
  • Infinities are useful in relational operations.
  • Null represents a piece of unknown missing data.
  • The null function returns true, if it's argument is null. However since most types in kdb+ have a null value, we can use equality to test for null, in contrast to standard SQL.

Summary

The important information to take from this tutorial is

  • How to define:
    • Numeric Data
    • booleans / bytes
    • Characters
    • Date, Times, Timestamps, Months, Hours
  • Accessing / Casting datetime values using dot notation
  • That most types in kdb+ have nulls and infinities