williey
Technical User
- Jan 21, 2004
- 242
I'm trying to generate random 7 digit numbers and then convert the result to char or varchar. Everything the below function executes, it only return 1 digit number. If I define the @result as int, I have no problem generating 7 digit long numbers.. The results are the same when using either "cast" or "convert". Coming from Oracle, I'm trying to learn SQL Server syntax..
------------------------------------------
There are 10 kinds of people in this world. One that understands binary and the other one that does not.
Code:
ALTER FUNCTION [dbo].[fn_generatenumber]
(
-- Add the parameters for the function here
@ltype char
)
RETURNS varchar
AS
BEGIN
-- Declare the return variable here
DECLARE @Result varchar
DECLARE @Random int
DECLARE @Lower int
DECLARE @Upper int
-- Add the T-SQL statements to compute the return value here
SET @Lower = 1
SET @Upper = 9999999
SELECT @Random = ((@Upper - @Lower -1) * RandNumber + @Lower) from vw_randomnumber
SET @Result = cast(@Random as varchar)
RETURN @Result
END
------------------------------------------
There are 10 kinds of people in this world. One that understands binary and the other one that does not.