I tried this :
SELECT TIMESTAMPADD(SECOND, FLOOR(RAND() * TIMESTAMPDIFF(SECOND, 2017-01-01', '2017-02-01 ';)), 2017-01-01 );
I want to generate randomly date from 1 of month to 30
尝试这个
SELECT TIMESTAMPADD(SECOND, FLOOR(RAND() * TIMESTAMPDIFF(SECOND, '2013-01-07', '2013-12-24 ')), '2013-01-07')
SELECT DATE_FORMAT(FROM_UNIXTIME(
ROUND((RAND() * (UNIX_TIMESTAMP('2013-12-24')-UNIX_TIMESTAMP('2013-01-07')))+UNIX_TIMESTAMP('2013-01-07'))
),'%Y-%m-%d')
For first UNIX_TIMESTAMP()
function, use higher date, for second and third UNIX_TIMESTAMP()
function, use lower date.
In MS SQL:
DECLARE @StartDT DATE = '2017-01-01' , @EndDT DATE = '2017-01-31'
SELECT DATEADD(DAY, nbr - 1, @StartDT) FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY c.object_id ) AS Nbr FROM sys.columns c ) nbrs WHERE nbr - 1 <= DATEDIFF(DAY, @StartDT, @EndDT)
well I think this is solution
SELECT '2017-01-01' + INTERVAL ROUND(RAND() * (30-1) + 1) DAY
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.