zoukankan      html  css  js  c++  java
  • SQL 创建随机时间的函数

     1 set ANSI_NULLS ON
     2 set QUOTED_IDENTIFIER ON
     3 go
     4 
     5 ALTER function [dbo].[fn_Randtime]   
     6 (  
     7  @begin_date datetime,  --開始時間
     8  @end_date datetime     --結束時間
     9 )  
    10 returns Datetime  
    11 as  
    12 begin  
    13   
    14 declare @second varchar(50)  
    15 declare @Holiday int
    16 declare @Holiday2 int
    17 if @begin_date is null  
    18  SET @begin_date=DateAdd(month,-1,cast((convert(varchar(7),getdate(),121)+'-01 08:01:01') as datetime));  
    19 if @end_date is null  
    20  SET @end_date=DateAdd(month,-1,cast((convert(varchar(7),getdate(),121)+'-28 17:30:00') as datetime));  
    21 SET @second = DATEDIFF ( second , @begin_date,@end_date)  
    22   
    23 declare @d1 datetime  
    24 declare @rand float 
    25 declare @SunHoliDay int
    26 declare @Extended datetime 
    27 declare @MaxHoliDay datetime
    28  
    29 select @rand=re from v_RAND 
    30 set @d1 =dateadd(second,@rand*@second,@begin_date)
    31 Select @HoliDay=Count(Holiday) From Topmixdata.dbo.HolidayCalendar Where Holiday=convert(varchar(10),@d1,120) And Status=0 
    32 
    33 --除去假日順延時間
    34 Select @SunHoliDay=Count(Holiday) From Topmixdata.dbo.HolidayCalendar where Convert(varchar(7),Holiday,120)=Convert(varchar(7),@begin_date,120) And datepart(weekday,Holiday)=1
    35 if @SunHoliDay>0
    36 begin
    37 SELECT @MaxHoliDay=dateadd(DAY,1,Max(Holiday)) FROM Topmixdata.dbo.HolidayCalendar where Convert(varchar(7),Holiday,120)=Convert(varchar(7),@begin_date,120)
    38 Set @Extended=@MaxHoliDay
    39 end
    40 
    41 --除去假日時間
    42 while @HoliDay>0 OR datepart(weekday,@d1)=1 OR datepart(weekday,@d1)=7 OR (Convert(varchar(10),@d1,120)=Convert(varchar(10),@Extended,120))
    43 begin
    44     select @rand=re from v_RAND 
    45     set @d1 =dateadd(second,@rand*@second,@begin_date)
    46     Select @HoliDay2=Count(Holiday) From Topmixdata.dbo.HolidayCalendar Where Holiday=convert(varchar(10),@d1,120) And Status=0 
    47     set @HoliDay=@HoliDay2
    48 end
    49 
    50 --if datepart(weekday,@d1)=1
    51 --begin
    52 --  set @d1=dateadd(dd,2,@d1)
    53 --end
    54 --if datepart(weekday,@d1)=7
    55 --begin
    56 --    if datepart(dd,@d1)=1
    57 --    set @d1=dateadd(dd,2,@d1)
    58 --    else
    59 --    set @d1=dateadd(dd,-1,@d1)
    60 --end
    61 
    62 if datepart(hour,@d1) >17
    63 begin  
    64   set @d1=dateadd(hour,-8,@d1)
    65 end  
    66 if datepart(hour,@d1) <8  
    67 begin  
    68   set @d1=dateadd(hour,8,@d1)     
    69 end  
    70 if datepart(hour,@d1)>11 and datepart(hour,@d1)<14
    71 begin
    72   set @d1=dateadd(hour,-2,@d1)
    73 end
    74 if datepart(hour,@d1)=17 and datepart(mi,@d1)>30
    75 begin
    76   set @d1=dateadd(mi,-30,@d1)
    77 end
    78 
    79 return @d1  
    80 end 
  • 相关阅读:
    Hive 2.1.1安装配置
    vi / vim 删除以及其它命令
    『MySQL』时间戳转换
    update 中实现子查询
    hive lateral view 与 explode详解
    xpath定位方法详解
    Python int与string之间的转化
    ORM、SQLAchemy
    python bottle 简介
    python wsgi 简介
  • 原文地址:https://www.cnblogs.com/slmdr9/p/5842846.html
Copyright © 2011-2022 走看看