Simple Query with string

Hello,

Im trying to create a new column(boolean) based on the string value of another column. Heres an example query I’m trying to use.

select has_name:?[not name=“”;1b;0b] from t1

Assume that t1 has a column name which is a string that contains names. Im trying to create a column “has_name” that is a boolean and checks if name exists or not. 

I dont want to do it like this:

select has_name:{x=“”}each name from t1  

Hi,

You could use the each right (/:) iterator along with ~

select hasname: not “” ~/: name from t1

Using ~ (match) instead of = here is preferable because = requires the two strings to be the same length. ~ also has the added benefit of directly returning either 0 or 1 regardless of the length of the two strings being compared. Another way to do this without an iterator would be to use the like keyword:

update hasname: not name like “” from t1

like also has the advantage of being compatible with string patterns and wild cards, for example if you wanted to find all the people in the list with the same first name.

Also, just fyi, it’s a good idea to avoid using _ in your column names (or variables in general) as it may get confused with the drop _ operator. If you want clarity for multi-word column or variable names you could try using camel casing (e.g. hasName). 

Hope that helps.  

q)update hasname:boolean$count each name from t1

`t1

I have here interpreted trying to create a new column to mean you want a new column in t1.

count each name returns a vector of string lengths; casting them to boolean flags the non-empty strings.

q)`boolean$count each(“quick”;“”;“brown”;“fox”)
1011b

HTH

Stephen




Stephen Taylor | Librarian | Kx | +44 7713 400852 | stephen@kx.com

Thank you all for your help! This works.