首页 > 代码库 > sql:日期操作注意的,如果以字符串转日期时的函数,因为数据量大,会出问题
sql:日期操作注意的,如果以字符串转日期时的函数,因为数据量大,会出问题
---1.以日期字符操作转换日期 如果是VIP1生日不对,可以以上传的数据日期为生日begindeclare @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 datetimedeclare @smonth varchar(2),@sday varchar(2)set @vipno=‘174030‘set @now=getdate()select @birthday=birthday from vip1 where vipno=@vipno and birthday is not nullif isdate(@birthday)=0 or @birthday=‘1900-01-01‘begin select @birthday=VipBirthdayDate from VipBirthdayAward where VipBirthdayNo=@vipnoendset @year=Year(@now)if month(@now)=1 and month(@birthday)=12begin set @year=@year-1endif month(@now)=1 and month(@birthday)=11begin set @year=@year-1endif month(@now)=2 and month(@birthday)=12 begin set @year=@year-1endset @month=month(@birthday)set @day=day(@birthday)set @smonth=cast(@month as varchar(2))select @smonthset @sday=cast(@day as varchar(2))if len(@smonth)<2begin set @smonth=‘0‘+@smonth select @smonthendif len(@sday)<2begin set @sday=‘0‘+@sdayend set @str=cast(@year as varchar(4))+‘/‘+@smonth+‘/‘+@sday+‘ 00:00:00‘set @NowBirthday=convert(datetime,@str) -----cast(@str as datetime)if isdate(@NowBirthday)=0begin select @NowBirthday=‘2014/01/01 00:00:00‘endselect @NowBirthdayendGO---2 时间加减操作SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) begindeclare @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 datetimedeclare @smonth varchar(2),@sday varchar(2)set @vipno=‘174030‘set @now=getdate()set @setyear=@nowselect @birthday=birthday from vip1 where vipno=@vipno and birthday is not nullif isdate(@birthday)=0 or @birthday=‘1900-01-01‘begin select @birthday=VipBirthdayDate from VipBirthdayAward where VipBirthdayNo=@vipnoendset @year=Year(@now)if month(@now)=1 and month(@birthday)=12begin set @year=@year-1 set @setyear=DATEDIFF(year,-1,@now) select @setyearendif month(@now)=1 and month(@birthday)=11begin set @year=@year-1 set @setyear=DATEDIFF(year,-1,@now) select @setyearendif month(@now)=2 and month(@birthday)=12 begin set @year=@year-1 set @setyear=DATEDIFF(year,-1,@now) select @setyearendselect @birthday as ‘birthday‘set @month=month(@birthday)select @monthset @setyear=DATEADD(year, DATEDIFF(year,0,@setyear), 0)set @setyear=Dateadd(month,@month-1,@setyear)select @setyearset @day=day(@birthday)set @setyear=Dateadd(day,@day-1,@setyear)select @NowBirthday=@setyearif isdate(@NowBirthday)=0begin select @NowBirthday=DATEADD(yy, DATEDIFF(yy,0,getdate()), 0) endselect @NowBirthdayendGO
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。