lundi 15 octobre 2018

Generate random number in Bigquery legacy

I want to get a random 4 digit integer in BigQuery. I tried the classic

SELECT CAST(10000*RAND() AS INTEGER) as number but it's giving 0

Adding this question as the results might surprise programmers used to CAST doing a TRUNC in most other languages.

SELECT word, CAST(round(10000*RAND(1)) AS integer) as rand
FROM [publicdata:samples.shakespeare]
order by rand
#Sample size needed = 10
limit 10

I am getting result as zero enter image description here




Aucun commentaire:

Enregistrer un commentaire