0
In the database I’m working on there are a number of salary variables that are registered as strings with entries like 0000001155,00
. I’m using a combination of CAST
and REPLACE
to turn these variables into float. For only one variable, I used:
CAST(REPLACE(vl_salario_contratual, ",", ".") AS float64) as wage_formatted
It turns out that I would like to perform this procedure for all the variables that present the same problem. I read in this question that doesn’t exist FOR LOOP
in SQL, so I tried to use the WHILE LOOP
, indicated in the answers, along with an array with the names of the variables I intend to change. Additionally, I read the documentation Big Query to adapt response code to BQ syntax.
Follows my code:
DECLARE
cnt INT64;
DECLARE
wage_vars ARRAY<STRING>;
SET
cnt=1;
SET
wage_vars=( vl_salario_contratual,
vl_remun_media_nom );
WHILE
@cnt < 3 DO CAST(REPLACE(@wage_vars[@cnt], ",", ".") AS float64) #erro ocorre aqui
SET
@cnt = @cnt + 1;
END
The program indicates that the error occurs in @wage_vars[@cnt]
and therefore this form of indexing is not accepted. I tried an alternative method after reading this question. Follows code:
DECLARE
wage_vars ARRAY<STRING>;
SET
wage_vars=( vl_salario_contratual,
vl_remun_media_nom );
SELECT * FROM wage_vars WHERE id IN(1,2)
CAST(REPLACE(wage_vars[id], ",", ".") AS float64)
Also unsuccessful.
How do I access elements from an array within a while loop in SQL?
I asked a similar question on the Soen and suggested using the
execute
. Link to the question: https://stackoverflow.com/questions/65383330/how-to-apply-a-user-defined-function-to-multiple-columns-in-standard-sql/65384727#65384727– Lucas