How to convert a symbol to a string in kdb+? - kdb

For example, if I have a list of symbols i.e (`A.ABC;`B.DEF;`C.GHI) or (`A;`B;`C), how could I convert each item in the list to a string?

string will convert them. It's an atomic function
q)string (`A.ABC;`B.DEF;`C.GHI)
"A.ABC"
"B.DEF"
"C.GHI"

You can use the keyword string to do this documented here
q)lst:(`A;`B;`C)
// convert to list of strings
q)string lst
,"A"
,"B"
,"C"

As the others have mentioned, string is what you're after. In your example if you're interested in separating the prefix and suffix separated by the . you can do
q)a:(`A.ABC;`B.DEF;`C.GHI)
q)` vs' a
A ABC
B DEF
C GHI
and if you want to convert these to strings you can just use string again on the above.

q)string each (`A.ABC;`B.DEF;`C.GHI)
"A.ABC"
"B.DEF"
"C.GHI"

Thanks all, useful answers! While I was trying to solve this on my own in parallel, I came across ($) that appears to work as well.
q)example:(`A;`B;`C)
q)updatedExample:($)example;
q)updatedExample
enlist "A"
enlist "B"
enlist "C"

use String() function.
q)d
employeeID firstName lastName
-----------------------------------------------------
1001 Employee 1 First Name Employee 1 Last Name
1002 Employee 2 First Name Employee 2 Last Name
q)update firstName:string(firstName) from `d
`d
q)d
employeeID firstName lastName
-------------------------------------------------------
1001 "Employee 1 First Name" Employee 1 Last Name
1002 "Employee 2 First Name" Employee 2 Last Name

Related

String match in Postgresql

I am trying to make separate columns in my query result for values stored in in a single column. It is a string field that contains a variety of similar values stored like this:
["john"] or ["john", "jane"] or ["john", "john smith', "jane"],etc... where each of the values in quotes is a distinct value. I cannot seem to isolate just ["john"] in a way that will return john and not john smith. The john smith value would be in a separate column. Essentially a column for each value in quotes. Note, I would like the results to not contain the quotes or the brackets.
I started with:
Select name
From namestbl
Where name like %["john"]%;
I think this is heading in the wrong direction. I think this should be in select instead of where.
Sorry about the format, I give up trying to figure out the completely useless error message when i try to save this with table markdown.
Your data examples represent valid JSON array syntax. So cast them to JSONB array and access individual elements by position (JSON arrays are zero based). The t CTE is a mimic of real data. In the illustration below the number of columns is limited to 6.
with t(s) as
(
values
('["john", "john smith", "jane"]'),
('["john", "jane"]'),
('["john"]')
)
select s::jsonb->>0 name1, s::jsonb->>1 name2, s::jsonb->>2 name3,
s::jsonb->>3 name4, s::jsonb->>4 name5, s::jsonb->>5 name6
from t;
Here is the result.
name1
name2
name3
name4
name5
name6
john
john smith
jane
john
jane
john

kdb+/q table - convert string to number

assume you have a table
tbl:([] id:("123"; ""; "invalid"))
And want to parse this string into a number.
Invalid values - in the example above, both the empty string "" as well as the value "invalid", should be parsed to null (0Nj).
How can you best do it? My initial approach was
select id:.[value;;0Nj] each enlist each id from tbl
But while that will parse the both the "123" as well as "invalid" entries correctly, it will return the unary operator :: instead of null when trying to parse the row with the empty string.
Of course I could do something like
select id:.[value;;0Nj] each enlist each id from update id:string (count id)#`invalid from tbl where id like ""
but this seems kind of.. ugly/inefficient. Is there any better way to do this?
Thanks
Try "J"$ to cast the column
q)select "J"$id from tbl
id
---
123
https://code.kx.com/v2/ref/tok/
how about just cast it to long?
q)update id:"J"$id from `tbl
`tbl
q)select from tbl where not null id
id
---
123

Firebird- Select specific word from string of words

I have a String of words/address ..
ID | ADDRESS
1 barangay1, City Province
2 barangay2, City Province
what i want to do is to select only the barangay without City and province
ID | ADDRESS
1 barangay1
2 barangay2
I tried using Position() but it only returns integer .. Can someone help
In addition to the POSITION() function you need the SUBSTRING() function to extract the part of the string. If the string you're intrested in is from the start of the string till the first comma then
select SUBSTRING(ADDRESS from 1 for POSITION(',' in ADDRESS)-1) from T
should work. You also might want to run the result throught TRIM() function to get rid of any leading and/or trailing whitespace.

Return only capitalised names from a SQL query

I have a table storing first names and surnames; some may be stored with capitalisation. Is there a query I could use to return only those rows with capitalisation?
For instance, if I have the following entries:
firstname | surname
-----------+-----------
Bob | Jones
john | bobbins
I'd only expect to be returned the record for "Bob Jones".
I'm sure it's not a difficult thing to do, but I haven't been able to find any examples anywhere.
Compare the value with the value where the first character is upper-case:
select *
from the_table
where firstname = initcap(firstname)
and surname = initcap(surname);
The function initcap() converts the first letter of each word to upper case and the rest to lower case.

Get substring into a new column

I have a table that contains a column that has data in the following format - lets call the column "title" and the table "s"
title
ab.123
ab.321
cde.456
cde.654
fghi.789
fghi.987
I am trying to get a unique list of the characters that come before the "." so that i end up with this:
ab
cde
fghi
I have tried selecting the initial column into a table then trying to do an update to create a new column that is the position of the dot using "ss".
something like this:
t: select title from s
update thedot: (title ss `.)[0] from t
i was then going to try and do a 3rd column that would be "N" number of characters from "title" where N is the value stored in "thedot" column.
All i get when i try the update is a "type" error.
Any ideas? I am very new to kdb so no doubt doing something simple in a very silly way.
the reason why you get the type error is because ss only works on string type, not symbol. Plus ss is not vector based function so you need to combine it with each '.
q)update thedot:string[title] ss' "." from t
title thedot
---------------
ab.123 2
ab.321 2
cde.456 3
cde.654 3
fghi.789 4
There are a few ways to solve your problem:
q)select distinct(`$"." vs' string title)[;0] from t
x
----
ab
cde
fghi
q)select distinct(` vs' title)[;0] from t
x
----
ab
cde
fghi
You can read here for more info: http://code.kx.com/q/ref/casting/#vs
An alternative is to make use of the 0: operator, to parse around the "." delimiter. This operator is especially useful if you have a fixed number of 'columns' like in a csv file. In this case where there is a fixed number of columns and we only want the first, a list of distinct characters before the "." can be returned with:
exec distinct raze("S ";".")0:string title from t
`ab`cde`fghi
OR:
distinct raze("S ";".")0:string t`title
`ab`cde`fghi
Where "S " defines the types of each column and "." is the record delimiter. For records with differing number of columns it would be better to use the vs operator.
A variation of WooiKent's answer using each-right (/:) :
q)exec distinct (` vs/:x)[;0] from t
`ab`cde`fghi