我正在使用c#和sql在MVC4中完成我的项目..我有一个表MemberDetails
,其中包含表
CREATE TABLE [dbo].[MemberDetails] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Mem_FirstNA] VARCHAR (100) NOT NULL,
[Mem_LastNA] VARCHAR (100) NOT NULL,
[Mem_Occ] VARCHAR (100) NOT NULL,
[Mem_DOB] DATETIME NOT NULL,
[Mem_Email] VARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
我只想选择未来30天生日的姓名和出生日期,并使用以下查询
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE
Mem_DOB >= getdate() - 1 AND Mem_DOB <= getdate() + 30
这是否正确,我选择了0项,我使用下表。
1 Pal Software 08-03-1987 AM 12:00:00
3 mn Par Bussiness 19-10-1967 AM 12:00:00
4 man George Business 13-11-1956 AM 12:00:00
5 Smi Kan Housewife 22-10-1980 AM 12:00:00
答案 0 :(得分:2)
更新
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE
DATEDIFF(DAY, GETDATE(), DATEADD(YEAR, DATEDIFF(YEAR, Mem_DOB, GETDATE()), Mem_DOB)) BETWEEN 0 AND 30
答案 1 :(得分:2)
试试这个
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE DAYOFYEAR(Mem_DOB)-DAYOFYEAR(getdate())<=30
答案 2 :(得分:2)
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE
Mem_DOB >= Cast(current_timestamp As Date) AND Mem_DOB < DATEADD(d, 30, Cast(current_timestamp As Date));
答案 3 :(得分:1)
应该有效。根据评论删除使用之间。 也适用于1月份的出生日期
DECLARE @now DATETIME
SET @now = GETDATE()
SELECT Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM MemberDetails
WHERE
CASE WHEN month(Mem_DOB) = 1
THEN DATEADD(YY, YEAR(DATEADD(DAY, 30, DATEADD(m, month(@now) - 1, DAY(@now) - 1))) - 1900, DATEADD(m, month(Mem_DOB) - 1, DAY(Mem_DOB) - 1))
ELSE DATEADD(m, month(Mem_DOB) - 1, DAY(Mem_DOB) - 1)
END
> DATEADD(m, month(@now) - 1, DAY(@now) - 1)
AND
CASE WHEN month(Mem_DOB) = 1
THEN DATEADD(YY, YEAR(DATEADD(DAY, 30, DATEADD(m, month(@now) - 1, DAY(@now) - 1))) - 1900, DATEADD(m, month(Mem_DOB) - 1, DAY(Mem_DOB) - 1))
ELSE DATEADD(m, month(Mem_DOB) - 1, DAY(Mem_DOB) - 1)
END
< DATEADD(DAY, 30, DATEADD(m, month(@now) - 1, DAY(@now) - 1))
希望有所帮助
答案 4 :(得分:0)
试试这个
SELECT
Mem_FirstNA, Mem_LastNA, Mem_DOB
FROM
MemberDetails
WHERE
WHERE DATEPART(mm,Mem_DOB) BETWEEN DATEPART(mm,getDate()) AND DATEPART(mm,getDate())+1;
答案 5 :(得分:0)
为什么不使用where where子句:
WHERE MEM_DOB BETWEEN GETDATE()-1 AND GETDATE()+30
答案 6 :(得分:-2)
试试这个......
SELECT
Mem_FirstNA,Mem_LastNA,Mem_DOB
从
MemberDetails
哪里
ltrim(str(year(GETDATE())))+' - '+ ltrim(str(month(Mem_DOB)))+' - '+ ltrim(str(day(Mem_DOB)))&gt; = getdate() - 1 AND ltrim(str(year(GETDATE())))+' - '+ ltrim(str(month(Mem_DOB)))+' - '+ ltrim(str(day(Mem_DOB)))&lt; = getdate()+ 30 德尔>
Select Mem_FirstNA, Mem_LastNA, Mem_DOB from MemberDetails m, (
Select datepart(dd,getdate()) as d, datepart(mm,getdate()) as m
union
Select datepart(dd,getdate() + 1) as d, datepart(mm,getdate() + 1) as m
union
Select datepart(dd,getdate() + 2) as d, datepart(mm,getdate() + 2) as m
union
Select datepart(dd,getdate() + 3) as d, datepart(mm,getdate() + 3) as m
union
Select datepart(dd,getdate() + 4) as d, datepart(mm,getdate() + 4) as m
union
Select datepart(dd,getdate() + 5) as d, datepart(mm,getdate() + 5) as m
union
Select datepart(dd,getdate() + 6) as d, datepart(mm,getdate() + 6) as m
union
Select datepart(dd,getdate() + 7) as d, datepart(mm,getdate() + 7) as m
union
Select datepart(dd,getdate() + 8) as d, datepart(mm,getdate() + 8) as m
union
Select datepart(dd,getdate() + 9) as d, datepart(mm,getdate() + 9) as m
union
Select datepart(dd,getdate() + 10) as d, datepart(mm,getdate() + 10) as m
union
Select datepart(dd,getdate() + 11) as d, datepart(mm,getdate() + 11) as m
union
Select datepart(dd,getdate() + 12) as d, datepart(mm,getdate() + 12) as m
union
Select datepart(dd,getdate() + 13) as d, datepart(mm,getdate() + 13) as m
union
Select datepart(dd,getdate() + 14) as d, datepart(mm,getdate() + 14) as m
union
Select datepart(dd,getdate() + 15) as d, datepart(mm,getdate() + 15) as m
)X
where
datepart(dd, m.Mem_DOB) = x.d and datepart(mm, m.Mem_DOB) = x.m
如果您正在进行投票,请评论原因。