简体   繁体   中英

is there a way to extract 70% of character from a string using bigquery?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM