Home > Back-end >  is there a way to extract 70% of character from a string using bigquery?
is there a way to extract 70% of character from a string using bigquery?

Time:12-10

I have column with Names of various length like :

Name      | ID
Avi       | 01
Li        | 02
Amandeep  | 03

I want to extract 70% of characters.

I am using : substring(Name,1, (length(Name)-5))

But this does not work when length(name) is less than 2 or 3

CodePudding user response:

I think you want:

SELECT Name, ID, SUBSTRING(Name, 1, CAST(CEIL(0.7*LENGTH(Name)) AS INT64)) AS Name70Pct
FROM yourTable;

Here we are taking 70% of the length of the name. I wrap with CEIL() to ensure that a name of one character will at least return that one character.

  • Related