[SQL] Stored ProcedureTo Generate Random Time of Day

Here is a function that will generate a random time of day. Later I will show why I am posting this particular query and how I am using it. It comes in useful when trying create random scheduled jobs in SQL Server. I made use of a technique for generating random dates via Jon Galloway.

Parameter DataType Extra Description
@timeOfDay int OUTPUT Represents a time of day using the format HHMMSS. For example, midnight is represented as 0 and 1:52:01 PM is represented as 135201.
@maxHour int Default = 24 Upper bound for the hour of the day. So to generate a random time of day between midnight and 3 AM, specify 3.

Here is an example of its usage.

-- Generate time of day between midnight and 3
DECLARE @timeOfDay int
exec GetRandomTimeOfDay @timeOfDay OUTPUT, 24
PRINT @timeOfDay

And here is the stored procedure declaration itself. Notice I am creating this procedure within the master database.

USE [master]
GO

CREATE PROCEDURE [dbo].GetRandomTimeOfDay
( 
    @timeOfDay int = 0 OUTPUT
    , @maxHour int = 24 -- Upper bound for the hour.
)
AS
BEGIN
    IF @maxHour > 24 OR @maxHour < 1
        RAISERROR ('Choose value between 1 and 24', 16, 1)   
    
    DECLARE @randomHours int
    SELECT @randomHours = 
        (@maxHour - 1) * 
RAND(CAST(CAST(newid() as binary(8)) as INT))
    
    DECLARE @randomMinutes int
    SELECT @randomMinutes = 
        60 * RAND(CAST(CAST(newid() as binary(8)) as INT))
    
    DECLARE @timeOfDayDate DateTime
    SET @timeOfDayDate = '00:00:00'
    
    SET @timeOfDayDate = 
DATEADD(hh, @randomHours, @timeOfDayDate)
    SET @timeOfDayDate = 
DATEADD(mi, @randomMinutes, @timeOfDayDate)
    
    DECLARE @timeAsString varchar(8)
    DECLARE @timeWithoutColons varchar(6)
    
    SET @timeAsString = CONVERT(varchar(8), @timeOfDayDate, 8)
    SET @timeWithoutColons = REPLACE(@timeAsString, ':', '')
    
    SET @timeOfDay = ( CAST(@timeWithoutColons as int) )
END
GO

In a later installment, I will show you why this is useful.

You can download the SQL in a zip file

Technorati Tags: ,

What others have said

Requesting Gravatar... cheese frenzy Jun 24, 2007 8:47 PM
# re: [SQL] Stored ProcedureTo Generate Random Time of Day
nice one! much appreciated :)
Requesting Gravatar... towps Nov 26, 2008 1:16 AM
# re: [SQL] Stored ProcedureTo Generate Random Time of Day
select DATEADD(second, rand()*86400, DATEADD(d, rand()*(CAST((@date_to - @date_from) AS INT )), @date_from))
Requesting Gravatar... Jason Feb 13, 2011 1:09 PM
# re: [SQL] Stored ProcedureTo Generate Random Time of Day
This can actually be used in lots of different situations. Thanks for posting.

Jason, the iPhone developer

What do you have to say?

(will show your gravatar)
Please add 8 and 6 and type the answer here: