Last active
November 27, 2019 12:56
-
-
Save erkineren/ebc23feda1af62c905aca5d7e20e44d7 to your computer and use it in GitHub Desktop.
MySQL Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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