博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql: 生日三个月内有效
阅读量:6902 次
发布时间:2019-06-27

本文共 3579 字,大约阅读时间需要 11 分钟。

DECLARE @birthday  datetime,@stat datetime,@end datetime,@statbirthday datetime,@endbirthday datetime,@thirdbirthday datetime,@firthbirthday datetime, @year int,@month int , @day int,@str varchar(20),@total int,@firthmonth int,@thirmonth int,@now  datetime,@vipno nvarchar(10) --参数set @vipno='8888888'set @now='2014-01-25'set @year=Year(@now)select  @birthday=birthday from vip1 where vipno=@vipnoset @month=month(@birthday)set @day=day(@birthday)set @str=cast(@year as varchar(4))+'-'+cast(@month as varchar(2))+'-'+cast(@day as varchar(2))set @birthday=cast(@str as datetime)--會員生日有效生首日起至3個月內有效set @firthbirthday=DATEADD(mm, DATEDIFF(mm,0,@birthday)-2, 0)--DATEADD(mm, DATEDIFF(mm,0,@birthday), 0)  --当月的第一天--set @thirdbirthday=DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+3, 0)) --后三个月最后的一天SELECT @firthbirthday=DATEADD(MONTH,-2,DATEADD(MONTH,DATEDIFF(MONTH,0,@birthday),0))--select cast(@firthbirthday as datetime)--set @firthbirthday=DATEADD(MONTH,-2,DATEADD(MONTH,DATEDIFF(MONTH,0,@birthday),0))--DATEADD(Month,0,CONVERT(datetime,CONVERT(char(8),getdate(),120)+'1'))--select @firthbirthday--DATEADD(mm,0, DATEDIFF(mm,0,@birthday), 0)  --前三个月的第一天--set @firthbirthday=convert(datetime, @thirdbirthday)--SELECT  三個月有效 塗聚文 涂聚文set @firthmonth=month(@now)-2set @thirmonth=month(@now)set @thirdbirthday=DATEADD(ms,-3,DATEADD(mm, DATEDIFF(mm,0,@now)+1, 0)) --当月最后的一天set @thirdbirthday=cast(@thirdbirthday as datetime)set @statbirthday=DATEADD(mm, DATEDIFF(mm,0,@birthday)-13, 0)set @endbirthday=DATEADD(ms,-3,DATEADD(mm, DATEDIFF(mm,0,@birthday)-1, 0))--select @statbirthday,@endbirthday,@firthbirthday,@thirdbirthday,@birthday,@firthmonth,@thirmonth--以月份來吧SELECT  *  FROM View_birthdayVipdlyList WHERE DATEDIFF(month,NowBirthday,@now)>=0 and  DATEDIFF(month,NowBirthday,@now)<=2  and vipno=@vipno order by VipBirthdayDate desc--SELECT  *  FROM vip1 WHERE indate>@statbirthday AND  indate<@endbirthday and  month(birthday)>=@firthmonth and month(birthday)<=@thirmonth --and vipno=@vipno--SELECT @total=ISNULL(SUM(amount),0) FROM View_birthdayVipdlyList WHERE indate>@statbirthday AND  indate<@endbirthday and  month(birthday)>=@firthmonth and month(birthday)<=@thirmonth and vipno=@vipno--SELECT @total=@total*.01 SELECT  count(*) as 'total'  FROM View_birthdayVipdlyList WHERE DATEDIFF(month,NowBirthday,getdate())>=0 and  DATEDIFF(month,NowBirthday,getdate())<=2  and vipno=@vipnoif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetVipNowBirthday]') and xtype in (N'FN', N'IF', N'TF'))drop function [dbo].[GetVipNowBirthday]GOCREATE   function  GetVipNowBirthday(     @vipno nvarchar(10), --参数   @now datetime)returns datetimeASbegindeclare @NowBirthday datetime, @birthday  datetime,@stat datetime,@end datetime,@statbirthday datetime,@endbirthday datetime,@thirdbirthday datetime,@firthbirthday datetime, @year int,@month int , @day int,@str varchar(40),@total decimalselect @birthday=birthday from vip1 where vipno=@vipno and birthday is not nullset @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 @str=cast(@year as varchar(4))+'-'+cast(@month as varchar(2))+'-'+cast(@day as varchar(2))set @NowBirthday=convert(datetime,@str) ---cast(@str as datetime)return @NowBirthdayendGOselect top 100  vipno,birthday,[dbo].[GetVipNowBirthday](vipno,'2014-03-23') as 'now' from dbo.vip1 where month(birthday)=12 or month(birthday)=11

 

转载地址:http://ivldl.baihongyu.com/

你可能感兴趣的文章
C#编程(二十一)----------扩展方法
查看>>
BZOJ 2141: 排队 [CDQ分治]
查看>>
netty5入门教程
查看>>
SecureCRT 连接虚拟机Linux
查看>>
你是否也忘了刷新视图?
查看>>
C语言 · 数的划分
查看>>
C# 超高速高性能写日志 代码开源
查看>>
js-jquery-SweetAlert【二】配置方法
查看>>
反向传播公式
查看>>
轻松学习Ionic (二) 为Android项目集成Crosswalk(更新官方命令行工具)
查看>>
c 变量的存储类型auto等(基础知识)和c函数变量
查看>>
树莓派的GPIO编程
查看>>
sql server 2008如何导入mdf,ldf文件
查看>>
让Asp.Net WebAPI支持OData查询,排序,过滤。(转)
查看>>
在centos中安装mangodb
查看>>
Windows Dos命令下查看端口号,杀死端口
查看>>
VS------csc.exe已停止工作解决方法
查看>>
PHP mysql_fetch_array与mysql_fetch_row的区别
查看>>
Python ORM框架之 Peewee入门
查看>>
Windows版Mycat结合mysql安装配置+水平切分(转载)
查看>>