Home > Software engineering >  SQL - get the first X words of a string
SQL - get the first X words of a string

Time:10-02

I want to 'cut' customer reviews (strings) to the first 50 words, in Biq Query SQL. In the end to display in data studio only these 50 first words in a table. Help someone ? thanks

CodePudding user response:

One option would be to find the position of the 50th space symbol and do substring:

select if(instr(mycolumn, ' ', 1, 50) > 0, substr(mycolumn, 1, instr(mycolumn, ' ', 1, 50) - 1), mycolumn) 
from mytable

CodePudding user response:

Try This

SELECT left(colname,50);

for eg.

SELECT 
    left("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.",50);

Output - enter image description here

  • Related