Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Google Sheets Query – How to return part of String?

I’m using the following formula, to successfully retrieve data.

=QUERY(IMPORTRANGE(...);"select Col5, Col1, Col2 where Col2!=''";FALSE)

The data in Col2 are strings that look like this: A thing - something - some other thing

Only the part before the first occurence of - is of interest; but I’m having trouble only returning this part of the string.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I use the following formula on one of the strings, I get the desired result:

=REGEXEXTRACT("A thing - something - some other thing";"^[^-]*")

I’m not sure how to combine the formula’s however; or if this is even the right way to go about this?
All help is greatly appreciated!

>Solution :

You can create a new column applying REGEXEXTRACT (with the help of BYROW to do it sequentially) and then use that inside the QUERY.

I’ll use LET so it’s easier to see the process:

=LET(imported;IMPORTRANGE(...);
newcol;BYROW(INDEX(imported;;2),LAMBDA(each;REGEXEXTRACT(each;"^[^-]*")))
QUERY({imported\newcol};"select Col5, Col1, Col6 where Col6!=''";FALSE))

I’m assuming that your imported range has 5 columns, so the new one is the 6th; please adapt it if necessary

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading