As the question suggests.
I need to randomly generate an Integer at the end of my SQL query. It is supposed to be part of a code that I have to create from existing variables.
SELECT LEFT(Var1,2) & RIGHT(Var2,1) & RND
FROM MyTable;
The method I used here generates a number that is a decimal like 0,2895625 at the end of the code. I need it to generate a whole Integer.
Thanks Again!
答案 0 :(得分:2)
DECLARE @maxval TINYINT, @minval TINYINT
select @maxval=24,@minval=5
SELECT CAST(((@maxval + 1) - @minval) *
RAND(CHECKSUM(NEWID())) + @minval AS TINYINT)
Use this. set @maxval and @minval according to your need.
Or try this.
SELECT CAST(RAND() * 1000000 AS INT)
Or you can try this.
Declare @Rnd INT;
set @Rnd = CAST(RAND() * 1000000 AS INT)
SELECT LEFT(Var1,2) & RIGHT(Var2,1) & @Rnd
FROM MyTable;
or
Declare @Rnd INT;
set @Rnd = ABS(CHECKSUM(NewId())) % 10000
SELECT LEFT(Var1,2) & RIGHT(Var2,1) & @Rnd
FROM MyTable;