SQL獲取隨機時間

2022-07-14 09:21:07 字數 663 閱讀 9982

在sql裡 datetime和money可以互相轉換,如果我們要獲取乙個時間段內的隨機時間,就可以借助money來產生了,首先,獲取起止時間轉換後的money值,

select   cast(cast('2012-01-01 00:00:00'   as   datetime)   as   money)  

---40907.00

select   cast(cast('2012-10-16 8:30:30'   as   datetime)   as   money) 

--41196.3545

然後以最小值為基準,獲取中間的乙個隨機數

40907+(cast(rand()*2893545 as int))*1.0000/10000

再轉換成時間 就得到這個隨機時間了

select cast(40907+(cast(rand()*2893545 as int))*1.0000/10000 as datetime)

但是在做測試資料 需要一次插入多條時 會發現所有的隨機時間都相同,這也好辦,只要在rand()裡加上checksum(newid())即可,最後的sql語句就是

select cast(40907+(cast(rand(checksum(newid()))*2893545 as int))*1.0000/10000 as datetime)

sql 新增時間段內隨機時間

網上搜的例子 時間段內 declare dt1 datetime,dt2 datetime,a bigint,b bigint set dt1 2010 01 01 開始日期 set dt2 2010 06 30 結束日期 select a cast dt1 asbigint b cast dt2 ...

按秒生成隨機時間

或者用如下函式 if exists select from dbo.sysobjects where id object id n dbo f randtime and xtype in n fn n if n tf drop function dbo f randtime go if exists...

MFC 獲取本機時間 CTime

框1,框2,框3 static text控制項,修改id m strdate 關聯框1,cstring資料型別 value類別 m strtime 關聯框2,cstring資料型別 value類別 m strdatetime 關聯框3,cstring資料型別 value類別 獲取按鈕訊息響應函式 v...