Share via


find the range between two decimal numbers

Question

Thursday, September 28, 2017 6:26 AM

 i receive two decimal values from frontend,
 a=0.98 or 01.02
 b=01.02 or 0.98

 i want to generate range between that two numbers

 expected output-0.98,0.99,01.00,01.01,01.02 

note that 

  i want to generate the range between two decimal numbers

All replies (6)

Thursday, September 28, 2017 6:56 AM

See your other thread for how to: how to create number  using query

Olaf Helper

[ Blog] [ Xing] [ MVP]


Thursday, September 28, 2017 7:23 AM

HOW TO SELECT THE VALUE OR GENERATE THE VALUE BETWEEN TWO DECIMAL NUMBERS


Thursday, September 28, 2017 7:36 AM

With a recursive CTE

DECLARE @from decimal(15, 2) = 0.98;
DECLARE @to decimal(15, 2) = 1.02;

;WITH cte AS
    (SELECT @from AS Value
     UNION ALL
     SELECT CONVERT(decimal(15, 2), Value + 0.01)
     FROM cte
     WHERE Value < @to)
SELECT *
FROM cte

Olaf Helper

[ Blog] [ Xing] [ MVP]


Thursday, September 28, 2017 7:54 AM

select number*.01 from master..spt_values where number*.01 between .98 and 1.02

Thursday, September 28, 2017 8:49 AM

Hi,

You have already posted two threads before this one about one same question.

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/9ec9740f-f6b7-402c-953b-65d61d8e39df/converting-withctc-code-into-normal-sql-query?forum=transactsql#fcd8a7f6-c7d7-4484-8137-f21aa4e2b28a

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/7fc93157-43ce-4d7f-a497-6d260b1f6b20/how-to-create-number-using-query?forum=transactsql 

Do not post duplicated threads. We have already provided you two kinds of solutions. One is using recursive CTE and one is using loop. If you don't want to use CTE, you can try loop.

Won't the two solutions resolve your requirement? If so, please tell us why.

Thanks,
Xi Jin.

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Thursday, September 28, 2017 4:05 PM

Hi,

Hope the below query helps:

declare @a float =0.98 , @b float=01.02 , @i float=0.01
declare @t table (result float)
while (@a <= @b)
begin
insert @t select @a
set @a=@a+@i
end

select * from @t

Thanks & Regards,

Praveena KK.