vordan
4/16/2017 - 11:03 AM

Generate random date between two dates in MySQL

Generate random date between two dates in MySQL

Generically, FLOOR(RAND() * (<max> - <min> + 1)) + <min> 
generates a number between <min> and <max> inclusive.

-- Between 17 and 44

select floor(rand() * (44 - 17 + 1)) + 17;
select 
	concat(
		'0', floor(rand() * (79 - 70 + 1)) + 70, '/', 
		floor(rand() * 401) + 100, '-', 
		floor(rand() * 401) + 100
	) 
;
select date_format(
    from_unixtime(
         rand() * 
            (unix_timestamp('1967-11-13 16:00:00') - unix_timestamp('1990-11-13 23:00:00')) + 
             unix_timestamp('1990-11-13 23:00:00')
                  ), '%Y-%m-%d %H:%i:%s') as datum_roden
;
select
	concat(round(rand() * 10000),
			 round(rand() * 10000),
			 round(rand() * 10000),
			 round(rand() * 10000)
			) as rnd
;