zoukankan      html  css  js  c++  java
  • sql:日期操作注意的,如果以字符串转日期时的函数,因为数据量大,会出问题

    ---1.以日期字符操作转换日期 如果是VIP1生日不对,可以以上传的数据日期为生日
    begin
    declare @NowBirthday datetime, @birthday  datetime,@stat datetime,@end datetime,@statbirthday datetime,@endbirthday datetime,@thirdbirthday datetime,@firthbirthday datetime, @year int,@month int , @day int,@str varchar(80),@total decimal,@vipno nvarchar(10),@now datetime
    
    declare @smonth varchar(2),@sday varchar(2)
    set @vipno='174030'
    set @now=getdate()
    select @birthday=birthday from vip1 where vipno=@vipno and birthday is not null
    if isdate(@birthday)=0 or @birthday='1900-01-01'
    begin
    	select  @birthday=VipBirthdayDate from VipBirthdayAward where VipBirthdayNo=@vipno
    end
    set @year=Year(@now)
    if month(@now)=1 and month(@birthday)=12
    begin
       set @year=@year-1
    end
    if month(@now)=1 and month(@birthday)=11
    begin
       set @year=@year-1
    end
    if month(@now)=2 and month(@birthday)=12 
    begin
       set @year=@year-1
    end
    set @month=month(@birthday)
    set @day=day(@birthday)
    set @smonth=cast(@month as varchar(2))
    select @smonth
    set @sday=cast(@day as varchar(2))
    if len(@smonth)<2
    begin
      set @smonth='0'+@smonth
      select @smonth
    end
    if len(@sday)<2
    begin
      set @sday='0'+@sday
    end 
    set @str=cast(@year as varchar(4))+'/'+@smonth+'/'+@sday+' 00:00:00'
    set @NowBirthday=convert(datetime,@str) -----cast(@str as datetime)
    if isdate(@NowBirthday)=0
    begin
       select @NowBirthday='2014/01/01 00:00:00'
    end
    select @NowBirthday
    end
    GO
    
    ---2 时间加减操作
    SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) 
    
    
    begin
    declare @NowBirthday datetime, @birthday  datetime,@stat datetime,@end datetime,@statbirthday datetime,@endbirthday datetime,@thirdbirthday datetime,@firthbirthday datetime, @year int,@month int , @day int,@str varchar(80),@total decimal,@vipno nvarchar(10),@now datetime,@setyear datetime
    
    declare @smonth varchar(2),@sday varchar(2)
    set @vipno='174030'
    set @now=getdate()
    set @setyear=@now
    select @birthday=birthday from vip1 where vipno=@vipno and birthday is not null
    if isdate(@birthday)=0 or @birthday='1900-01-01'
    begin
    	select  @birthday=VipBirthdayDate from VipBirthdayAward where VipBirthdayNo=@vipno
    end
    set @year=Year(@now)
    if month(@now)=1 and month(@birthday)=12
    begin
       set @year=@year-1
       set @setyear=DATEDIFF(year,-1,@now)
       select @setyear
    end
    if month(@now)=1 and month(@birthday)=11
    begin
       set @year=@year-1
       set @setyear=DATEDIFF(year,-1,@now)
       select @setyear
    end
    if month(@now)=2 and month(@birthday)=12 
    begin
       set @year=@year-1
       set @setyear=DATEDIFF(year,-1,@now)
       select @setyear
    end
    select @birthday as 'birthday'
    set @month=month(@birthday)
    select @month
    set @setyear=DATEADD(year, DATEDIFF(year,0,@setyear), 0)
    set @setyear=Dateadd(month,@month-1,@setyear)
    select @setyear
    
    set @day=day(@birthday)
    set @setyear=Dateadd(day,@day-1,@setyear)
    select @NowBirthday=@setyear
    if isdate(@NowBirthday)=0
    begin
       select @NowBirthday=DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) 
    end
    select @NowBirthday
    end
    GO
    
    
  • 相关阅读:
    Java的参数传递是值传递还是引用传递
    10张图带你深入理解Docker容器和镜像
    Java 如何有效地避免OOM:善于利用软引用和弱引用
    事务与一致性:刚性or柔性
    Java 面试题史上最强整理
    三张图秒懂Redis集群设计原理
    iOS开发笔记系列-基础4(变量与数据类型)
    iOS开发笔记系列-基础3(多态、动态类型和动态绑定)
    iOS开发笔记系列-基础2(类)
    iOS开发笔记系列-基础1(数据类型与表达式)
  • 原文地址:https://www.cnblogs.com/geovindu/p/3851224.html
Copyright © 2011-2022 走看看