Randomise Date Column

This is a useful statement for when you need help randomizing your date data for testing purposes. In this case, I’m assuming you’ve inserted a certain amount of test data into your table but it’s all showing the same date.

First of all: temporarily disable any triggers that you have that will affect this table.

Modify this statement to fit your needs:

update
   TABLE_NAME
set
   DATE_COLUMN = TO_DATE(TRUNC(DBMS_RANDOM.value(TO_CHAR(date '2018-03-06','J'),TO_CHAR(CURRENT_TIMESTAMP,'J'))),'J');

This will generate random dates between the two date parameters you’ve set. One thing to note though is that it isn’t changing the HH:MM:SS of the data, only the day. If you want those randomized as well you’d need to change the truncate value to whatever you needed it to be.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s