Skip to content
Advertisement

How to add up a string of numbers using SQL (BigQuery)?

I have a string of numbers like this:

670000000000100000000000000000000000000000000000000000000000000

I want to add up these numbers which in the above example would result in 14: 6+7+0+...+1+0+...+0+0+0=14

How would I do this in BigQuery?

Advertisement

Answer

Consider below approach

with example as (
  select '670000000000100000000000000000000000000000000000000000000000000' as s
)
select s, (select sum(cast(num as int64)) from unnest(split(s,'')) num) result
from example       

with output

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement