sql: 生日三个月内有效
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: 生日三个月内有效的更多相关文章
- SQLServer学习笔记<>sql的范围内查找,sql数据类型,字符串处理函数
sql的范围内查找 (1)between.....and用法 通常情况下我们查找一个在某固定区域内的所有记录,可以采用>=,<=来写sql语句,例如:查找订单价格在1000到2000之间的 ...
- SQL Server中内连接和外连接的区别
SQL Server中内连接和外连接的区别 假设一个数据库中有两张表,一张是学生表StudentInfo,一张是班级表ClassInfo,两张表之间用ClassId字段进行关联. 如果用内连接,正常的 ...
- 你真的会玩SQL吗?内连接、外连接
原文:你真的会玩SQL吗?内连接.外连接 大多数人一般写多表查询会这样写select * from tbA ,tbB 没有用到JOIN关键字,太Low了,官网标准建议是用JOIN明确表间的关系,下面 ...
- SQL连接(内连接、外连接、交叉连接)
SQL连接(内连接.外连接.交叉连接) 假设现在有两个表:table1 , table2 table1: table2 : id ...
- SQL中的内连接与外连接
关于关系代数连接运算的介绍请查看下面链接 http://www.cnblogs.com/xidongyu/articles/5980407.html 连接运算格式 链接运算由两部分构成:连接类型和连接 ...
- Sql server中内连接语句
数据库中学生表和课程表如下: 内连接sql语句: select a.studentName,a.studentAge,b.courseName from student a inner join co ...
- 如何用SQL实现组内前几名的输出
关于问题 如何查询组内最大的,最小的,大家或许都知道,无非是min.max的函数使用.可是如何在MySQL中查找组内最好的前两个,或者前三个? 什么是相关子查询 在提出对于这个问题的对应方法之前,首先 ...
- sql中的内联和外联(简单用法)
有两张表:user和department User表: CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` ...
- SQL连接:内连接、外连接、交叉连接。
SQL连接可以分为内连接.外连接.交叉连接. 数据库数据: book表 stu表 1.内连接 ...
随机推荐
- SQL Server 堆表与栈表的对比(大表)
环境准备 使用1个表,生成1000万行来进行性能对比(勉强也算比较大了),对比性能差别. 为了简化过程,不提供生成随机数据的过程.该表初始为非聚集索引(堆表),测试过程中会改为聚集索引(栈表). CR ...
- 【HTML】Beginner8:Table
1.Table Abused to lay out pages The correct use for tables is to do exactly what you would ex ...
- oracle rac IP详解
rac环境下vip/public/private IP的区别 每个节点要2块网卡, 3个IP,虚拟IP或者叫做业务IP,单个网卡当掉可以“漂”到其他网卡是继续提供服务 在Oracle RAC环境下,每 ...
- FZU 2122 又见LKity
直接模拟或者KMP #include <iostream> #include <string.h> #include <stdio.h> #include < ...
- globalfifo设备驱动
把globalmem中的全局内存变成一个FIFO,只有当FIFO中有数据的时候(即有进程把数据写到这个FIFO而且没有被读进程读空),读进程才能把数据读出,而且读取后的数据会从globalmem的全局 ...
- HW2.17
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- CodeForces 352C. Jeff and Rounding(贪心)
C. Jeff and Rounding time limit per test: 1 second memory limit per test: 256 megabytes input: stan ...
- Android UI开发第三十二篇——Creating a Navigation Drawer
Navigation Drawer是从屏幕的左侧滑出,显示应用导航的视图.官方是这样定义的: The navigation drawer is a panel that displays the ap ...
- PTA 08-图7 公路村村通 (30分)
现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数NN(\le 1000≤1000)和候选道 ...
- DevExpress GridControl 显示行号、设置行号宽
显示行号类 /// <summary> /// GridView 显示行号 设置行号列的宽度 /// </summary> /// <param name="g ...