Skip to content

Instantly share code, notes, and snippets.

@erkineren
Last active November 27, 2019 12:56
Show Gist options
  • Save erkineren/ebc23feda1af62c905aca5d7e20e44d7 to your computer and use it in GitHub Desktop.
Save erkineren/ebc23feda1af62c905aca5d7e20e44d7 to your computer and use it in GitHub Desktop.
MySQL Snippets
CREATE FUNCTION `random_datetime_in_range`(
`min_date` TIMESTAMP,
`max_date` TIMESTAMP
)
RETURNS TIMESTAMP
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'Returns new timestamp between %min_data% and %max_date%'
BEGIN
SET @_max = UNIX_TIMESTAMP(max_date);
SET @_min = UNIX_TIMESTAMP(min_date);
RETURN FROM_UNIXTIME(FLOOR(RAND()*(@_max-@_min+1)+@_min));
END;
CREATE FUNCTION `random_int`(
`min_val` INT,
`max_val` INT
)
RETURNS INT
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'Returns random integer between %min_val% and %max_val%'
BEGIN
RETURN FLOOR(RAND()*(max_val-min_val+1)+min_val);
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment