1

I have the following code for a single column table that has values from 0 to 9:

create table number(
    value int
)

truncate table number

insert into number values(0);
insert into number values(1);
insert into number values(2);
insert into number values(3);
insert into number values(4);
insert into number values(5);
insert into number values(6);
insert into number values(7);
insert into number values(8);
insert into number values(9);

What I want is a query which should join the numbers without using concat (or any builtin function) function and gives me the following output in a tabular form:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
.
.
.
.
.
999

Really having a hard time doing it for the past 5 days. Need sincere help.

2
  • We can help you a lot more effectively if you post what you tried. Otherwise it just looks like you want someone to write code for you, do your homework, pass the interview test, ... . Commented Dec 27, 2015 at 15:15
  • It looks like you are just trying to generate the numbers from 0-999. Is there a reason you are not just using a larger numbers table? Commented Dec 27, 2015 at 18:16

2 Answers 2

4

You could use CROSS JOIN and simple Math formula:

SELECT [num] = n1.value * 100 + n2.value * 10 + n3.value 
FROM number n1, number n2, number n3
ORDER BY num

LiveDemo

Output:

╔═════╗
║ num ║
╠═════╣
║ 0   ║
║ 1   ║
║ 2   ║
║ 3   ║
║ 4   ║
║ 5   ║
║ 6   ║
║ 7   ║
║ ... ║
║ 999 ║
╚═════╝

EDIT:

Cross join will return records(tuples) (0,0,0), (0,0,1),(0,0,2), ...,(9,9,9) then you need to apply Positional Number System formula:

enter image description here

example:

enter image description here

Images from https://en.wikipedia.org/wiki/Positional_notation

╔════════════╦═══════════╦══════════╦════════════════════╗
║ 10^2 = 100 ║ 10^1 = 10 ║ 10^0 = 1 ║        num         ║
╠════════════╬═══════════╬══════════╬════════════════════╣
║ 0 * 100    ║ 0 * 10    ║ 0 * 1    ║ 0 + 0 + 0 = 0      ║
║ 0 * 100    ║ 0 * 10    ║ 1 * 1    ║ 0 + 0 + 1 = 1      ║
║ 0 * 100    ║ 0 * 10    ║ 2 * 1    ║ 0 + 0 + 2 = 2      ║
║ ...        ║ ...       ║ ...      ║ ...                ║
║ 9 * 100    ║ 9 * 10    ║ 9 * 1    ║ 900 + 90 + 9 = 999 ║
╚════════════╩═══════════╩══════════╩════════════════════╝
Sign up to request clarification or add additional context in comments.

1 Comment

thanks alot brother. it really works but it would be a great favour if you explain the mathematics behind it.
0

While @lad2025 query is good based on certain formula.It uses 3 cross join.

Simpler way (not sure,if this is best)

Declare  @number table(
    value int
)

insert into @number values(0);
insert into @number values(1);
insert into @number values(2);
insert into @number values(3);
insert into @number values(4);
insert into @number values(5);
insert into @number values(6);
insert into @number values(7);
insert into @number values(8);
insert into @number values(9);

Declare @MaxLimit int=999
;

WITH CTE
AS (
    SELECT value
    FROM @number A

    UNION ALL

    SELECT a.value + 10
    FROM CTE A
    WHERE A.value < @MaxLimit-9
    )
SELECT value
FROM cte
--WHERE value <= 999
ORDER BY value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.