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=@vipno
set @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)-2
set @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=@vipno if 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]
GO
CREATE function GetVipNowBirthday
(
@vipno nvarchar(10), --参数
@now datetime
)
returns datetime
AS
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(40),@total decimal select @birthday=birthday from vip1 where vipno=@vipno and birthday is not null
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 @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 @NowBirthday
end
GO select top 100 vipno,birthday,[dbo].[GetVipNowBirthday](vipno,''2014-03-23'') as ''now'' from dbo.vip1 where month(birthday)=12 or month(birthday)=11

转自:http://www.16aspx.com/Article/3906

sql: 生日三个月内有效的更多相关文章

  1. SQLServer学习笔记<>sql的范围内查找,sql数据类型,字符串处理函数

    sql的范围内查找 (1)between.....and用法 通常情况下我们查找一个在某固定区域内的所有记录,可以采用>=,<=来写sql语句,例如:查找订单价格在1000到2000之间的 ...

  2. SQL Server中内连接和外连接的区别

    SQL Server中内连接和外连接的区别 假设一个数据库中有两张表,一张是学生表StudentInfo,一张是班级表ClassInfo,两张表之间用ClassId字段进行关联. 如果用内连接,正常的 ...

  3. 你真的会玩SQL吗?内连接、外连接

    原文:你真的会玩SQL吗?内连接.外连接 大多数人一般写多表查询会这样写select * from tbA ,tbB  没有用到JOIN关键字,太Low了,官网标准建议是用JOIN明确表间的关系,下面 ...

  4. SQL连接(内连接、外连接、交叉连接)

    SQL连接(内连接.外连接.交叉连接) 假设现在有两个表:table1 , table2 table1:                                table2 :  id     ...

  5. SQL中的内连接与外连接

    关于关系代数连接运算的介绍请查看下面链接 http://www.cnblogs.com/xidongyu/articles/5980407.html 连接运算格式 链接运算由两部分构成:连接类型和连接 ...

  6. Sql server中内连接语句

    数据库中学生表和课程表如下: 内连接sql语句: select a.studentName,a.studentAge,b.courseName from student a inner join co ...

  7. 如何用SQL实现组内前几名的输出

    关于问题 如何查询组内最大的,最小的,大家或许都知道,无非是min.max的函数使用.可是如何在MySQL中查找组内最好的前两个,或者前三个? 什么是相关子查询 在提出对于这个问题的对应方法之前,首先 ...

  8. sql中的内联和外联(简单用法)

    有两张表:user和department User表: CREATE TABLE `user` (    `id` int(11) NOT NULL AUTO_INCREMENT,    `name` ...

  9. SQL连接:内连接、外连接、交叉连接。

    SQL连接可以分为内连接.外连接.交叉连接. 数据库数据:             book表                                          stu表 1.内连接 ...

随机推荐

  1. ARM-Linux配置DHCP自动获取IP地址

    备注:内核版本:2.6.30.9busybox版本:1.15.2 PC Linux和开发板Linux的工作用户:root 1. 配置内核:[*] Networking support --->N ...

  2. 安装qc 出现error An error occurred while attempting to connect to the database.

    When trying to install mercury quality center starter edition 9.0 on Windows XP media center, I am g ...

  3. Asm Shader Reference --- Shader Model 2.x part

    ps部分 概览   Instruction Set                                       Name Description Instruction slots S ...

  4. Unity4.3.3 烘焙踩坑

    许久没发文章了,开始实习了,挺忙的基本没什么时间了 unity4.3.3是一个非常古老的版本了,弄了一下烘焙,踩了不少坑, 首先是unity自带的nature shader,其中有soft occlu ...

  5. IIS7 性能(内存、CPU、当前请求耗时)监测

    程序上线了,但运行过程中如果发现很CPU.内存异常,某些操作耗时,如何在生产环境中监测并查明原因呢,有以下几种工具(方式): 1.NProfiler,这是一个商业软件,有试用周期,可以监测包括Winf ...

  6. linkscrpit

    一 section是什么? 好吧,我们需要解释一下平时编译链接生成的二进制可执行程序(比如说ELF,EXE也行),so或者dll,内核(非压缩的,参加本系列第一节内容.vmlinux),或者ko是怎么 ...

  7. oc学习之路----QQ聊天界面

    用到的技术:自定义cell,通知机制,代理模式 transform 1.自定义cell(通过代码自定义cell) ①首先新建一个类继承UITableViewCell ②重写initWithStyle: ...

  8. 1、IIS常见的的问难及解决方法

    配置IIS 7.5 出现的问题及解决办法: 1.问题:  CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Tempor ...

  9. UVA 557 - Burger(概率 递推)

     Burger  When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was ...

  10. 在Visual Studio中使用Pseudovariables来帮助调试

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:在Visual Studio中使用Pseudovariables来帮助调试.