我试过了:
SELECT TIMESTAMPADD(SECOND, FLOOR(RAND() * TIMESTAMPDIFF(SECOND, 2017-01-01', '2017-02-01 ';)), 2017-01-01 );
我想从1月1日到30日随机生成日期
答案 0 :(得分:0)
试试这个
SELECT u.cmp, -- UF values preceding the TRL range
u.bnd_cd,
u.start_date,
LEAST( u.end_date, t.start_date )
FROM data t
INNER JOIN
data u
ON ( t.bnd_cd = 'TRL'
AND u.bnd_cd = 'UF'
AND t.cmp = u.cmp
AND u.start_date < t.start_date )
UNION ALL
SELECT u.cmp, -- UF values within the TRL range
u.bnd_cd,
GREATEST( u.start_date, t.start_date ),
LEAST( u.end_date, t.end_date )
FROM data t
INNER JOIN
data u
ON ( t.bnd_cd = 'TRL'
AND u.bnd_cd = 'UF'
AND t.cmp = u.cmp
AND u.start_date <= t.end_date
AND u.end_date >= t.start_date )
UNION ALL
SELECT u.cmp, -- UF values following the TRL range
u.bnd_cd,
GREATEST( u.start_date, t.end_date ),
u.end_date
FROM data t
INNER JOIN
data u
ON ( t.bnd_cd = 'TRL'
AND u.bnd_cd = 'UF'
AND t.cmp = u.cmp
AND u.end_date > t.end_date )
UNION ALL
SELECT t.cmp, -- TRL values preceding the UF range
t.bnd_cd,
t.start_date,
LEAST( t.end_date, u.start_date )
FROM data t
INNER JOIN
data u
ON ( t.bnd_cd = 'TRL'
AND u.bnd_cd = 'UF'
AND t.cmp = u.cmp
AND t.start_date < u.start_date )
UNION ALL
SELECT t.cmp, -- TRL values within the UF range
t.bnd_cd,
GREATEST( t.start_date, u.start_date ),
LEAST( t.end_date, u.end_date )
FROM data t
INNER JOIN
data u
ON ( t.bnd_cd = 'TRL'
AND u.bnd_cd = 'UF'
AND t.cmp = u.cmp
AND t.start_date <= u.end_date
AND t.end_date >= u.start_date )
UNION ALL
SELECT t.cmp, -- TRL values following the UF range
t.bnd_cd,
GREATEST( t.start_date, u.end_date ),
t.end_date
FROM data t
INNER JOIN
data u
ON ( t.bnd_cd = 'TRL'
AND u.bnd_cd = 'UF'
AND t.cmp = u.cmp
AND t.end_date > u.end_date )
答案 1 :(得分:0)
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')
对于第一个UNIX_TIMESTAMP()
函数,使用较高的日期,对于第二个和第三个UNIX_TIMESTAMP()
函数,请使用较低的日期。
答案 2 :(得分:0)
在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 在哪里 - 1&lt; = DATEDIFF(DAY,@ StartDT,@ EndDT)
答案 3 :(得分:0)
我认为这是解决方案
SELECT '2017-01-01' + INTERVAL ROUND(RAND() * (30-1) + 1) DAY