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
If you need 10 random numbers:
SELECT CAST(round(10000*RAND(1)) AS integer) as rand
FROM [publicdata:samples.shakespeare]
LIMIT 10
With #standardSQL
#standardSQL
SELECT CAST(round(10000*RAND()) AS INT64) as rand
FROM UNNEST(GENERATE_ARRAY(1,10))
The problem statement is not clear about the goal - but this is how you generate random numbers between 0 and 9999.
Related
I have a table with few columns including 2 varchar2(200) columns. In this columns we basically store serial numbers which can be numeric or alpha-numeric. Alpha-numeric values always both serials are same in those 2 columns. However for number serials it is a range like (first column value = 511368000004001226 and second column value = 511368000004001425 with 200 different (Qty)). Maximum length of the serial is 20 digits. I have indexed both the columns.
Now I want to sear a serial in-between the above range. (lets say 511368000004001227). I use following query.
SELECT *
FROM Table_Namr d
WHERE d.FROM_SN <= '511368000004001227'
AND d.TO_SN >= '511368000004001227'
Is it a valid query? Can I use <=> operators for numbers in a varchar column?
Yes, You can use >= and <= operators on Varchar2 columns but it will behave like it is string and comparison between strings will take place.
In this case, 4 will be considered greater than 34 means '4' > '34' but number 4 is less than 34.
It is not a good practice to store a number in Varchar2. You will lose the functionality of Numbers if you store them in varchar2.
You can check the above concept using following:
select * from dual where '4' > '34'; -- gives result 'X'
select * from dual where 4 > 34; -- Gives no result
You can try to convert the varchar2 column to number using to_number if possible in your case.
Cheers!!
Your Query is "valid" in the sense, that it works, and will deliver a result. If you are looking from a numeric standpoint, it will not work correctly, as the range operators for VARCHAR columns work the same way, as it would sort an alphanumeric value.
e.g.
d.FROM_SN >= '51000'
AND d.TO_SN <= '52000'
This would match for values, as you would expect, like 51001, 51700, but would also deliver unexpected values like 52, or 5100000000000000
If you want numeric selection, you would need to parse it - which of course only works, if every value in these columns is numeric:
TO_NUMBER(d.FROM_SN) >= 51000
AND TO_NUMBER(d.TO_SN) <= 52000
You may use alphanumerical comparison provided
1) your ranges are of the same length and
2) all the keys in the range are of the same length
Example data
SERNO
----------------------------------------
101
1011
1012
1013
1014
102
103
104
This doesn't work
select * from tab
where serno >= '101' and serno <= '102';
SERNO
----------------------------------------
101
102
1011
1012
1013
1014
But constraining the lentgh of the result provides the right answer
select * from tab
where serno >= '101' and serno <= '102'
and length(serno) = 3;
SERNO
----------------------------------------
101
102
The Oracle doc says one can store a number up to 9.99...9 x 10125 with up to 38 significant digits: https://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#i16209.
I tried this:
create table bigtest (t number(38,2));
insert into bigtest values (5e40);
But I got
[Error] Execution (8: 29): ORA-01438: value larger than specified precision allowed for this column
It is supposed to be able to store 9.99e125, right? Could any one give an example on how to store 9.99e125?
See DBfiddle here (Oracle 18c).
create table T1 (
anumber number
) ;
insert into t1 ( anumber ) values ( 9.99e125 ) ;
select * from t1 ;
ANUMBER
999000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
One way is to use the number data type without precision and scale specified.
You can specify precision and scale for very large (and also for very small) numbers though. Just keep in mind that negative scale means "that many zeros at the end of an integer" - the total number of digits can be up to precision + absolute value of scale.
In the example below, note that 38 + 84 = 122. The scale must be between -84 and 127, which means that if you do use precision and scale, you can only store numbers < 1e123 - a smaller range than for the full number data type, but still storing very large numbers
create table tbl(x number(38,-84));
insert into tbl values (3.493e121);
select x from tbl;
X
----------
3.4930E+121
Just started working with oracle using toad ide. trying to format the numbers from a table in specific format. the numbers come in from a variable in the table and I want to display the whole numbers as whole numbers and display floats as floats. So far, I can use trim(TO_CHAR (width,'999.999')) to display all numbers with decimal points.
For example: 123.5 will be displayed as 123.500 and 100 will be displayed as 100.000.
What I want to do is display for eg: 100 as 100.
Hope this is clear and I get a solution soon.
I'm using MOD for determining decimals.
select test_value, (case when mod(test_value,1) != 0 then 'DECIMAL' else 'NODECIMAL' END) IS_DECIMAL
from (select 1.5 test_value from dual
union all
select 100 test_value from dual) test_table
If your problem is about the way Toad shows numbers, you can follow the hints in the comments.
If the problem is about the way Oracle shows numbers, converting them to strings, maybe this can help:
SQL> select to_char(1.5, 'TM9') as num from dual union all
2 select to_char(100, 'TM9') from dual;
NUM
----------------------------------------------------------------
1,5
100
You find much more in the documentation
If you need a way to check whether a number has a decimal part or not, you can simply try:
SQL> with numbers(num) as (
2 select 1.5 from dual union all
3 select 100 from dual
4 )
5 select case
6 when floor(num) = num
7 then to_char(num, 'FM999999') || ' has not a decimal part'
8 else
9 to_char(num, 'FM9999D000') || ' has a decimal part'
10 end as checkString
11 from numbers;
CHECKSTRING
------------------------------
1,500 has a decimal part
100 has not a decimal part
I have a currency amount value like this;
22200000
I want to convert this number to;
22,2 (Number format)
How can I do this?
use to_char() function. Example
to_char(3510.78, '$9,999.00')
would return
$3,510.78
I found the answer: SELECT TO_CHAR (22200000 / 1000000, '999,999,999,999.99') FROM dual
You can use Oracle Built-in function round().
The ROUND function accepts a number and returns another number rounded
to the specified number of places to the right of the decimal point.
If you do not specify that number, ROUND will return a number rounded
to the nearest integer
For instance:
select 1/3, round(1/3, 2) from dual;
1/3 ROUND(1/3,2)
---------- ------------
.333333333 .33
More info: Working with Numbers in PL/SQL
I want to format
if integer, then integer.00
if float, then float.xx (2 digits of precision)
I have a division operation whose value may result in an integer or float. I want to store the result with 2 digits of precision. How can I achieve this?
You can use the round operator or simply format it if it really is output only.
round ( value/divisor , 2)
to_char( value/divisor ,'99,999,999,990.99')
Please remark the 0 before the decimal point. This makes a value below 1 look more pretty with a leading zero. eg. 0.55 instead of .55.
Example SQL Fiddle
create table test (dividend number,
divisor number,
result number,
result_rounded number);
insert into test values (100,10,null,null);
insert into test values (9,5,null,null);
insert into test values (10,15,null,null);
update test set result = dividend / divisor
,result_rounded = round(dividend/divisor,2);
select * from test;
Result:
DIVIDEND DIVISOR RESULT RESULT_ROUNDED
100 10 10 10
9 5 1.8 1.8
10 15 0.666666666667 0.67
But at the end when you try to output this then the formatting comes into play and the rounding does not make much difference.
Example SQL Fiddle
select to_char(result,'99,999,999,990.99'),
to_char(result_rounded,'99,999,999,990.99')
from test;
Result
10.00 10.00
1.80 1.80
0.67 0.67
Try this
SELECT TO_CHAR(1,'99,999,999,999.99') FROM DUAL; -- THIS GIVES 1.OO
SELECT TO_CHAR(1.3,'99,999,999,999.99') FROM DUAL; -- THIS GIVES 1.30
The repeated 99999 is just for BILLION formatting. For thousands, it should be 999,999.99