Generate range of numbers between two numbers
You could do it with a recursive CTE like this
;WITH temp AS
(
SELECT 1 AS ID
UNION ALL
SELECT t.ID + 1 FROM temp t
WHERE t.ID < 100000
) -- return table with id from 1 to 100000
SELECT t.ID, y.Color, y.Dwelling
FROM YourTable y
INNER JOIN temp t ON t.ID BETWEEN y.IdBegin AND y.IdEnd
OPTION (MAXRECURSION 0)
How do I create a list with numbers between two values?
Use range
. In Python 2, it returns a list directly:
>>> range(11, 17)
[11, 12, 13, 14, 15, 16]
In Python 3, range
is an iterator. To convert it to a list:
>>> list(range(11, 17))
[11, 12, 13, 14, 15, 16]
Note: The second number in range(start, stop)
is exclusive. So, stop = 16+1 = 17
.
To increment by steps of 0.5
, consider using numpy's arange()
and .tolist()
:
>>> import numpy as np
>>> np.arange(11, 17, 0.5).tolist()
[11.0, 11.5, 12.0, 12.5, 13.0, 13.5,
14.0, 14.5, 15.0, 15.5, 16.0, 16.5]
See: How do I use a decimal step value for range()?
How to generate numbers between two values
Remove LEVEL
from SELECT
and modify limits in dbms_random.value
:
SQL> select round(dbms_random.value(31, 35)) result
2 from dual
3 connect by level <= 15;
RESULT
----------
32
31
34
32
34
31
33
32
33
32
34
34
35
33
33
15 rows selected.
SQL>
How to create numbers range of numbers between two numbers?
The problem with your query is that both the anchor and the recursive part of the query must have the same number of columns (and identical datatypes). Your anchor has just one column (numberMin
), while the recursive part has 4 columns, so the query errors.
Consider:
with numcte as (
select id, name, numberMin, numberMax, numberMin newcol from mytable
union all
select id, name, numberMin, numberMax, newcol + 1
from numcte
where newcol < numberMax - 1
)
select * from numcte
The where
condition prevents the last value of newcol
to actually reach numberMax
- that's a bit unusual, but that's what your desired result is showing.
Note that SQL Server by default restricts the number of iteration of a recursive query to 100. If you need to handle more than that, you need to add option (maxrecursion 0)
at the end of the query.
Create a range between two numbers in JavaScript
As per Bob's comment it looks like you just need to get a random number between 0 and the array length (less the required span) for your lower limit and then add the required span back to it for the upper limit. e.g.
let myArray = [];
for (let i = 0; i<101; i++){
myArray.push({objectID:i})
}
let requiredSpan = 6;
let lowerBand = Math.floor(Math.random()*(myArray.length-requiredSpan));
let upperBand = lowerBand + requiredSpan-1;
console.log(lowerBand, upperBand);
How to generate a range of numbers between two alphanumeric values?
This is not as easy as it may seem. The length of the string prefix could vary; and the number part may have leading zeros.
Here is an approach using a recursive query. The idea is to first separate the sring prefix from the numeric suffix (I assumed everything after the first digit in the string), then generate the series of numbers, and finally concatenate back into string (with respect the the length of the original string).
declare @str1 nvarchar(max) = 'WAC01001';
declare @str2 nvarchar(max) = 'WAC01012';
with cte as (
select
n,
cast(substring(@str1, n, len(@str1)) as int) num,
cast(substring(@str2, n, len(@str2)) as int) end_num,
left(@str1, n - 1) prefix
from (select patindex('%[0-9]%', @str1) n) x
union all
select
n,
num + 1,
end_num,
prefix
from cte
where num < end_num
)
select concat(
prefix,
replicate('0', len(@str1) - n - len(num) + 1),
num
) res
from cte
order by num
If you need to handle more than 100 increments, then you need to add option (maxrecursion 0)
at the end of the query.
Demo on DB Fiddle:
| res |
| :------- |
| WAC01001 |
| WAC01002 |
| WAC01003 |
| WAC01004 |
| WAC01005 |
| WAC01006 |
| WAC01007 |
| WAC01008 |
| WAC01009 |
| WAC01010 |
| WAC01011 |
| WAC01012 |
Generate random number between two numbers in JavaScript
Important
The following code works only if the minimum value is `1`. It does not work for minimum values other than `1`.If you wanted to get a random integer between 1 (and only 1) and 6, you would calculate:
const rndInt = Math.floor(Math.random() * 6) + 1
console.log(rndInt)
How can I generate a list of numbers in between two parameters?
Here a simple solution:
range([],A,B):-
A > B.
range([A|T],A,B):-
A =< B,
A1 is A + 1,
range(T,A1,B).
First clause: if A
is greater than B
, the resulting list is empty ([]
). Second clause: if A
is less or equal than B
, increment A
by 1
and store the result in A1
, unify the head of the list with A
(see [A|T]
), and recursively call the predicate with the remaining part of the list (T
), A1
, and B
.
?- range(L,1,10).
L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
false
?- range(L,10,1).
L = []
false
Related Topics
Using Group by on Multiple Columns
How to Implement One-To-One, One-To-Many and Many-To-Many Relationships While Designing Tables
How to List All Foreign Keys Referencing a Given Table in SQL Server
SQL Server Process Queue Race Condition
Is a View Faster Than a Simple Query
What's the Difference Between Truncate and Delete in Sql
SQL Server Insert If Not Exists
The MySQL Extension Is Deprecated and Will Be Removed in the Future: Use MySQLi or Pdo Instead
How to Concatenate Multiple MySQL Rows into One Field
Sql, Auxiliary Table of Numbers
Error 1045 (28000): Access Denied For User 'Root'@'Localhost' (Using Password: Yes)
SQL Query to Concatenate Column Values from Multiple Rows in Oracle