过滤数据当前SQL Server当前

时间:2018-04-10 04:36:46

标签: sql sql-server sql-server-2016

请帮助,我正试图在本月展示我的数据。

这是我现在的查询:

select 
    IdEnt,
    convert(varchar(20),InputDate,103) as 'InputDate',
    userID,
    Nominal,
    TypeCost,
    convert(varchar(20),EntDate,103) as 'EntDate',
    EntParty,
    Premi,
    Div,
    RemarksAgent,
    RemarksFinance 
from 
    entReq 
where
    userID = $_SESSION[userID] 
order by 
    InputDate desc;

1 个答案:

答案 0 :(得分:2)

以下是使用EOMONTH

的一种方法
SELECT ident, 
       CONVERT(VARCHAR(20), inputdate, 103) AS 'InputDate', 
       userid, 
       nominal, 
       typecost, 
       CONVERT(VARCHAR(20), entdate, 103)   AS 'EntDate', 
       entparty, 
       premi, 
       div, 
       remarksagent, 
       remarksfinance 
FROM   entreq 
WHERE  userid = $_SESSION[userID]  
       AND inputdate >= Dateadd(dd, 1, EOMONTH(Getdate(), -1)) 
       AND inputdate < Dateadd(dd, 1, EOMONTH(Getdate())) 
ORDER  BY inputdate DESC; 

对于旧版本:

每月的第一天:DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)

每月最后一天:DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0))