Skip to content
Jonathan Bairstow edited this page Sep 8, 2020 · 6 revisions

This collection of SQL artefacts can be used to randomly generate date values, either singularly or in batches of up to 10000 values.

Reference data.

Bulk procedures described below generate values based on the values in [Reference].[Number]. This table can be populated by executing the procedure [Reference].[usp_Populate_Number].

Procedures and functions

[SingleValue].[ufn_Get_Date]

This function will return a random date value. As a function; this method can be used to easily assign a value to an variable, or to update a field on a table.

This function has the following input parameters:

Parameter Data Type Default Value
@MinDate date None
@MaxDate date None

Example usages:

Declare	@Date date

Select @Date = [SingleValue].[ufn_Get_Date] ('2020-01-01','2020-12-31')

[SingleValue].[usp_Get_Date]

This procedure will return a random date value. The generated value is returned via the output parameter @Date, and so can be captured in a variable or passed back to a calling application.

Parameter Data Type Default Value Direction
@MinDate date None Input
@MaxDate date None Input
@Date date None Ouptut

Example usages:

Declare @MinDate date, @MaxDate date, @NewDate date
Select	@MinDate = '1980-01-01',
	@MaxDate = getdate()

exec [SingleValue].usp_Get_Date @MinDate, @MaxDate, @NewDate out

[BulkValue].[usp_Get_Date]

This procedure will return up to 10000 random date values. This procedure has the following input parameter:

Parameter Data Type Default Value
@MinDate date None
@MaxDate date None
@QuantityRequired smallint None

A recordset with the following structure is returned:

Field Name Data Type
DateValue date

This method is useful for inserting bulk data directly in to a table, or as an ETL source.

Example Usage:

Declare @MinDate date, @MaxDate date, @QuantityRequired smallint
Select	@MinDate = '1980-01-01',
	@MaxDate = getdate(),
	@QuantityRequired = 1000

exec BulkValue.usp_Get_Date @MinDate, @MaxDate, @QuantityRequired

Clone this wiki locally