+ -
当前位置:首页 → 问答吧 → ASE 如何获得指定时间段内的随机时间?

ASE 如何获得指定时间段内的随机时间?

时间:2010-12-21

来源:互联网

比如

每天 23:00-23:59:59 时间范围内

取得一个随机时间

谢谢

作者: piml_lv   发布时间: 2010-12-21

  1. 日期函数

  2. getdate()

  3. 得到当前时间,可以设置得到各种时间格式.

  4. datepart(日期部分,日期)

  5. 取指定时间的某一个部分,年月天时分秒.

  6. datediff(日期部分,日期1,日期2)

  7. 计算指定的日期1和日期2的时间差多少.

  8. dateadd(日期部分,数值表达式,日期)

  9. 计算指定时间,再加上表达式指定的时间长度.

  10. --取时间的某一个部分

  11. select datepart(yy,getdate()) --year

  12. select datepart(mm,getdate()) --month

  13. select datepart(dd,getdate()) --day

  14. select datepart(hh,getdate()) --hour

  15. select datepart(mi,getdate()) --min

  16. select datepart(ss,getdate()) --sec

  17. --取星期几

  18. set datefirst 1

  19. select datepart(weekday,getdate()) --weekday

  20. --字符串时间

  21. select getdate() -- '03/11/12'

  22. select convert(char,getdate(),101) -- '09/27/2003'

  23. select convert(char,getdate(),102) -- '2003.11.12'

  24. select convert(char,getdate(),103) -- '27/09/2003'

  25. select convert(char,getdate(),104) -- '27.09.2003'

  26. select convert(char,getdate(),105) -- '27-09-2003'

  27. select convert(char,getdate(),106) -- '27 Sep 2003'

  28. select convert(char,getdate(),107) --'Sep 27, 2003'

  29. select convert(char,getdate(),10 --'11:16:06'

  30. select convert(char,getdate(),109) --'Sep 27 2003 11:16:28:746AM'

  31. select convert(char,getdate(),110) --'09-27-2003'

  32. select convert(char,getdate(),111) --'2003/09/27'

  33. select convert(char,getdate(),112) --'20030927'

  34. select rtrim(convert(char,getdate(),102))+' '+(convert(char,getdate(),10) -- '2003.11.12 11:03:41'

  35. --整数时间

  36. select convert(int,convert(char(10),getdate(),112)) -- 20031112

  37. select datepart(hh,getdate())*10000 + datepart(mi,getdate())*100 + datepart(ss,getdate()) -- 110646

  38. --时间格式 "YYYY.MM.DD HH:MI:SS" 转换为 "YYYYMMDDHHMISS"

  39. declare @a datetime,@tmp varchar(20),@tmp1 varchar(20)

  40. select @a=convert(datetime,'2004.08.03 12:12:12')

  41. select @tmp=convert(char(10),@a,112)

  42. select @tmp

  43. select @tmp1=convert(char(10),datepart(hh,@a)*10000 + datepart(mi,@a)*100 + datepart(ss,@a))

  44. select @tmp1

  45. select @tmp=@tmp+@tmp1

  46. select @tmp

  47. --当月最后一天

  48. declare

  49. @tmpstr varchar(10)

  50. @mm int,

  51. @premm int,

  52. @curmmlastday varchar(10)

  53. begin

  54. select @mm=datepart(month,getdate())--当月

  55. select @premm=datepart(month,dateadd(month,-1,getdate())) --上个月

  56. if (@mm>=1 and @mm<=

  57. select @tmpstr=convert(char(4),datepart(year,getdate()))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'

  58. else if (@mm>=9 and @mm<=11)

  59. select @tmpstr=convert(char(4),datepart(year,getdate()))+'.'+convert(char(2),datepart(month,dateadd(month,1,getdate())))+'.'+'01'

  60. else

  61. select @tmpstr=convert(char(4),datepart(year,dateadd(year,1,getdate())))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'

  62. select @curmmlastday=convert(char(10),dateadd(day,-1,@tmpstr),102) --当月最后一天

  63. end
复制代码


看看能不能帮到你!

作者: wfcjz   发布时间: 2010-12-23

select "23:"+convert(varchar(2),convert(int,60*rand()))+":"+convert(varchar(2),convert(int,60*rand()))

作者: Eisen   发布时间: 2010-12-23

再来个看起来形式更简单的。
select convert(varchar,dateadd(ss,rand()*3600,'23:00:00'),8)

作者: andkylee   发布时间: 2010-12-23

还是你这个漂亮……

作者: Eisen   发布时间: 2010-12-23