我正在使用 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)
 );

我只想选择 future 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

最佳答案

试试这个...
选择
Mem_FirstNA、Mem_LastNA、Mem_DOB

成员(member)详情
在哪里
ltrim(str(year(GETDATE()))) + '-' + ltrim(str(month(Mem_DOB))) + '-' + ltrim(str(day(Mem_DOB))) >= getdate() - 1 AND ltrim(str(year(GETDATE()))) + '-' + ltrim(str(month(Mem_DOB))) + '-' + ltrim(str(day(Mem_DOB)))
编辑:此答案的评论正确地指出,如果当前年份是闰年,它将不起作用。所以这次更新。使用 Get a list of dates between two dates using a function 可以更有效地生成日期列表

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

如果您投反对票,请评论原因。

关于sql - 如何从现在起从 SQL 表中获取 30 天,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19441808/

10-16 15:38