4.108 Cast

Format: s2n($str$), n2s($num$), n2b($num$), s2n($str$), s2d($str$), s2t($str$) s2b($str$), d2s($date$), d2t($date$), t2s($time$), t2d($time$), b2n($bool$), b2s($bool$)

Set of functions to convert data type. mcal do not automatically converts data type, user must specify the required data type conversion.

n2b(s2b) function converts true to 1("1") and false to 0("0"), other values are treated as NULL values. b2n(b2s) function converts true to 1("1") and false to 0("0").

d2t function converts date and time type data, and automatically completes the time as 12:00:00. d2s function converts data to 8 digit fixed length character string ("yyyymmdd"), t2s function converts data to 14 digit fixed length character string ("yyyymmddHHMMSS").

Refer "4.13 Date and Time Type" for more information on date and time.

Examples

Example 1: Fixed length random number

Generate random numbers from 1 to 9999 as 4 digit fixed length string. Integer data (results of randi) is not supported by fixlen function, thus the data must be converted to character string with n2s function.

$ more dat1.csv
id
1
2
3
4
$ mcal c='fixlen(n2s(randi(1,9999,11)),4,"R","0")' a=rsl i=dat1.csv o=rsl1.csv
#END# kgcal a=rsl c=fixlen(n2s(randi(1,9999,11)),4,"R","0") i=dat1.csv o=rsl1.csv
$ more rsl1.csv
id,rsl
1,1803
2,0684
3,0195
4,6647

Example 2: True false pattern

Detect unusual pattern in columns v1,v2,v3 and print as 01 in output.

$ more dat2.csv
id,v1,v2,v3
1,10,5,7
2,5,12,11
3,3,6,2
4,14,16,11
$ mcal c='cat("",b2s(${v1}>=10),b2s(${v2}>=10),b2s(${v3}>=10))' a=rsl i=dat2.csv o=rsl2.csv
#END# kgcal a=rsl c=cat("",b2s(${v1}>=10),b2s(${v2}>=10),b2s(${v3}>=10)) i=dat2.csv o=rsl2.csv
$ more rsl2.csv
id,v1,v2,v3,rsl
1,10,5,7,100
2,5,12,11,011
3,3,6,2,000
4,14,16,11,111