This post is a follow up to the previous post about datatypes in q. In that post, I went over all the datatypes available in q and how to identify them using character, numeric, or symbol representation. Now, we are ready to discuss another important topic in q: type casting.
This post is also available as a video on our YouTube channel:
Type casting refers to converting the underlying type of an atom from one type to another. How might you do that, you ask? Well, before we get to that, let’s first answer why might we want to do this? There are many reasons to be aware of the types of data you are dealing with and why you might want to convert from one type to another. For example, most operators or functions expect their arguments to be of a certain type or types. If you multiply “2” (type: char
) by 2 (type: int
), you will get a `type
error because the multiplication operator expects both arguments to be of one of the numeric types.
q)"2"*2 'type [0] "2"*2 ^
Similarly, you might want to convert type of some columns in your table from strings
to symbol
to efficiently store the data on disk. These are just two common examples of why you might want to cast your data from one type to another.
Now, let’s answer how you can type cast and the answer is, using the operator $
. $
takes two arguments. On left-hand side, is the desired type and on the right-hand side is the value, of which you want to convert the underlying type.
For example, to convert the type of 72.0 from float
to integer
, we can run:
q)`int$72.0 72i
Recall that there are three ways to represent data types: numeric, character, and symbol. All three of these can be used to pass the desired type when using $
.
For example, an integer can be represented as int
, i
and 6
. Thus, the following three statements are all equal:
q)`int$72.0 72i q)6h$72.0 72i q)"i"$72.0 72i
Here is an example of converting a float
to long
:
q)`long$72.0 72 q)7h$72.0 72 q)"j"$72.0 72
To create a symbol from string
, you can just use backtick `
q)`$"enlist[q]" `enlist[q]
While these examples make common sense, some type conversions are not so straightforward. For example, if you convert a character
to int
, you will get back the position of that character in the ASCII code. You can find the positions of different characters in the ASCII code on this website.
For example, converting character r
to int
returns 114
and converting 2
of character type to int
, surprisingly, returns 50
instead of 2
of integer
type.
q)`int$"r" 114i q)`int$"2" 50i
To get 2 of type integer
, we can use the uppercase character representation:
q)"I"$"2" 2i
Note, that the right argument has to be a string for uppercase I
to work. If you use a numerical type instead, you will get a `type
error:
q)"I"$2 'type [0] "I"$2
You can also use builtin function value
:
q)`int$ value "2" 2i
value
evaluates a list of characters as it would be evaluated if you were to type the content into the q interpreter.
Another useful function is string
which converts datatype to a list of characters. For example,
q)string 22 "22"
We will now discuss type casting with temporal datatypes which is very useful and can be used to extract specific information from temporal datatypes. For example, you can cast a date
type to a month
type to get the month from the date.
Here are some examples:
Recall that .z.p
gives us the the current UTC timestamp in nanoseconds. We will assign its value to the variable a
.
q)a:.z.p q)a 2018.11.29D01:13:20.319918000 / To extract date consisting of year, month and day q)`date$a 2018.11.29 / To extract years q)`year$a 2018i / To extract years and months q)`month$a 2018.11m / To extract months only q)`mm$a 11i / To extract days q)`dd$a 29i / To extract hours q)`hh$a 1i / To extract hours and minutes q)`minute$a 01:13 / To extract minutes only q)`uu$a 13i / to extract hours, minutes and seconds q)`second$a 01:13:20 / To extract seconds only q)`ss$a 20i
Additionally, converting a date
to int
gives us the number of days that have passed since the 2000.01.01
.
q)`int$2001.01.01 366i
Converting a timestamp
to long
gives you the number of nanoseconds since midnight.
q)`long$11:10:03.0000000000 40203000000000
That’s all we have for type casting for now. In this post, you learnt, what type casting is, different ways to cast one data type to another and why type casting is useful.