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. ArcGIS for Android示例解析之高亮要素-----HighlightFeatures

    转自:http://blog.csdn.net/wozaifeiyang0/article/details/7323606 HighlightFeatures 要素高亮化功能,相信有其他gis开发经营 ...

  2. JAX-WS(JWS)发布WebService

    WebService历来都很受重视,特别是Java阵营,WebService框架和技术层出不穷.知名的XFile(新的如CXF).Axis1.Axis2等. 而Sun公司也不甘落后,从早期的JAX-R ...

  3. CVirtualGridCtrl控件内的数据如何获取

    CVirtualGridCtrl控件是同花顺自己写的控件和网上的不同,难处理,可以通过 模拟输入ctrl+c,然后从clipboard提取内容.

  4. C# 日期转换为中文大写

    /// <summary> /// 日期转换为中文大写 /// </summary> public class UpperConvert { public UpperConve ...

  5. 如果iis的配置文件 applicationHost.config坏掉了, 会在 C:\inetpub\history\ 中存储历史备份。复制过去还原就可以了-摘自网络

    You will usually get the error ‘Configuration file is not well-formed XML’ ‘C:\Windows\system32\inet ...

  6. openStack 使用public key登陆

  7. StreamWriter

    public StreamWriter( string path, bool append ) 参数 path 类型:System.String要写入的完整文件路径. append 类型:System ...

  8. PC-飞起来!我的Windows XP——五步快速优化Windows XP

    虽然Microsoft的 Vista已经发售了快一年,但国内大部分系统用户仍使用着目前堪称完美的Windows XP.与以往的Windows操作系统一样,新安装的Windows XP可能还不在最佳状态 ...

  9. A Tour of Go Exercise: Loops and Functions

    As a simple way to play with functions and loops, implement the square root function using Newton's ...

  10. hdoj 3549 Flow Problem【网络流最大流入门】

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...