Skip to content

Instantly share code, notes, and snippets.

@NrI3
Created June 10, 2016 22:12
Show Gist options
  • Select an option

  • Save NrI3/154a924b31c5411a94cdf2cfd90a9fd4 to your computer and use it in GitHub Desktop.

Select an option

Save NrI3/154a924b31c5411a94cdf2cfd90a9fd4 to your computer and use it in GitHub Desktop.
Crea numeros aleatorios MSqlServer
------------------------
-- by Jsn
-- jason.scz@gmail.com
------------------------
go
create view vwRANB
as
select CAST(ROUND(RAND(),0) AS BIT) as Bln
go
create view vwRAND
as
select RAND() as Rnd
go
create function fnRAND(@min int,@max int)returns int
as
begin
declare @rnd as int
while @rnd<@min or @rnd>@max or @rnd is null
begin
if len(@max)>len(@min)
set @rnd = (select round(rnd*(power(10,len(@max))),0) from vwRAND)
else
set @rnd = (select round(rnd*(power(10,len(@min))),0) from vwRAND)
if @min < 0 or @max < 0
begin
declare @flag as int
set @flag = (select Bln from vwRANB)
if @flag>0
set @rnd = @rnd*-1
end
end
return @rnd
end
go
select dbo.fnRAND(0,2000)
select dbo.fnRAND(0,2)
select dbo.fnRAND(-55,7)
select dbo.fnRAND(-99,99)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment