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) -- Parameters 
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)
-- Member's birthday is valid from the first day of birth to 3 It's valid for six months
set @firthbirthday=DATEADD(mm, DATEDIFF(mm,0,@birthday)-2, 0)--DATEADD(mm, DATEDIFF(mm,0,@birthday), 0) -- The first day of the month
--set @thirdbirthday=DATEADD(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+3, 0)) -- The last day of the last three months
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) -- The first day of the first three months
--set @firthbirthday=convert(datetime, @thirdbirthday)
--SELECT Three months GeovinDu Tu Juwen
set @firthmonth=month(@now)-2
set @thirmonth=month(@now)
set @thirdbirthday=DATEADD(ms,-3,DATEADD(mm, DATEDIFF(mm,0,@now)+1, 0)) -- The last day of the month
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
-- Come in months
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), -- Parameters
@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

from :http://www.16aspx.com/Article/3906

sql: More related articles valid within three months of birthday

  1. SQLServer Learning notes &lt;&gt;sql Look up in the range of ,sql data type , String handling functions

    sql Look up in the range of (1)between.....and usage Usually we look up all the records in a fixed area , May adopt >=,<= To write sql sentence , for example : Find the order price in 1000 To 2000 Between ...

  2. SQL Server The difference between inner connection and outer connection

    SQL Server The difference between inner connection and outer connection Suppose there are two tables in a database , One is the student list StudentInfo, One is the class table ClassInfo, Between the two tables ClassId Fields are associated . If you use internal connections , natural ...

  3. You really know how to play SQL Do you ? Internal connection 、 External connection

    original text : You really know how to play SQL Do you ? Internal connection . External connection Most people write multi table queries like this select * from tbA ,tbB   Not used JOIN keyword , too Low 了 , The standard suggestion of the official website is to use JOIN Clarify the relationship between tables , below ...

  4. SQL Connect ( Internal connection 、 External connection 、 Cross connect )

    SQL Connect ( Internal connection . External connection . Cross connect ) Suppose there are now two tables :table1 , table2 table1:                                table2 :  id     ...

  5. SQL The inner connection and the outer connection in

    For an introduction to join operations in relational algebra, see the link below http://www.cnblogs.com/xidongyu/articles/5980407.html Join operation format The link operation consists of two parts : Connection type and connection ...

  6. Sql server The inner join statement

    The student list and curriculum in the database are as follows : Internal connection sql sentence : select a.studentName,a.studentAge,b.courseName from student a inner join co ...

  7. How to use SQL Achieve the output of the top few in the group

    About the problem How to query the largest , The smallest , You may all know , No more than is min.max The function uses . But how to MySQL Find the first two best in the group , Or the first three ? What is related subquery Before putting forward the corresponding method for this problem , First ...

  8. sql In and out in ( It is easy to use )

    There are two tables :user and department User surface : CREATE TABLE `user` (    `id` int(11) NOT NULL AUTO_INCREMENT,    `name` ...

  9. SQL Connect : Internal connection 、 External connection 、 Cross connect .

    SQL Connections can be divided into inner connections . External connection . Cross connect . Database data :             book surface                                           stu surface 1. Internal connection ...

Random recommendation

  1. json jsonp The difference between

      Preface : Speaking of AJAX Will inevitably face two problems , The first is AJAX How to exchange data ? The second is how to solve the cross domain requirements ? These two problems have different solutions at present , For example, you can use custom strings or XML To describe , Cross domain ...

  2. PHP js Use ajax Asynchronous processing mode requests PHP, Solve array Chinese garbled

    html End : <html> <head> <script type="text/javascript" src="jquery/jquery ...

  3. 【 primary / turn 】UITableview Performance optimization summary

    UITableView As ios One of the most frequently used controls in , Its performance optimization is often faced with , Especially when the amount of data is too large and the equipment performance is insufficient . The purpose of this paper is to summarize tableview Several performance optimizations of tips, And with the deepening of understanding , this paper ...

  4. ti processor sdk linux am335x evm /bin/unshallow-repositories.sh hacking

    #!/bin/bash # # ti processor sdk linux am335x evm /bin/unshallow-repositories.sh hacking # explain : # This article focuses on ...

  5. Bracket matching problem (C++、 Stack )

    Original address :http://www.cppblog.com/GUO/archive/2010/09/12/126483.html /* Bracket matching problem , Comparative classics , Use the stack to achieve ( Excerpt from internet) 1. ...

  6. Srtuts2 Realize the login interface ( Don't connect to the database ) Report errors ( Four )

    1. utilize Struts2 Write a login screen , There are the following problems March 01, 2014 12:26:18 Afternoon org.apache.struts2.dispatcher.Dispatcher warn Warning : ...

  7. 【 The Internet 】TCP/IP Connect three handshakes

    TCP handshake protocol stay TCP/IP Agreement ,TCP Protocol provides reliable connection service , Use three handshakes to establish a connection . The first handshake : When establishing a connection , The client sends syn package (syn=j) To the server , And enter SYN_SEND state , Wait for the server to confirm ...

  8. gdb debugging python

    gdb edition >7 Of Yes python Debugging has special support , Reference resources : https://docs.python.org/devguide/gdb.html?highlight=gdb https://bl ...

  9. Spring3.2+mybatis3.2+Struts2.3 Integrate

    1.Spring3.2 Cannot be used for JDK1.8, It can only be used for JDK1.7.JDK1.8 use spring4.0. 2. Imported jar package 3. Directory structure : 4. To configure Spring Configuration database information : <?xml ve ...

  10. Python Module introduction

    One . modular : A code set that implements a function with a piece of code . Two . There are three types of modules 1. Custom module 2. Built in standard module ( Also known as the standard library ) 3. Open source module ( Upload the way , Baidu PyPi) Open source module installation : a.yum b.pip c ...