如何在不更改日期SQL的情况下提取当前日期和时间

时间:2014-11-20 14:05:34

标签: sql-server

有人可以告诉我如何改变它,以便它总是拉动当天吗?

SELECT Groups.GroupName, AgentTeams.TeamName, [Agent.Firstname] & [ Agent.Lastname, CRC.Description, Count(History.HistoryID) AS [CRC Count]
FROM ((GroupAgent INNER JOIN Groups ON GroupAgent.GroupID = Groups.GroupID) INNER JOIN ((Agent LEFT JOIN History ON Agent.AgentID = History.AgentID) LEFT JOIN CRC ON History.CRC = CRC.CRC) ON  GroupAgent.AgentID = Agent.AgentID) INNER JOIN (AgentTeams INNER JOIN AgentStartDates ON  AgentTeams.TeamID = AgentStartDates.TeamID) ON GroupAgent.AgentID = AgentStartDates.AgentID
WHERE (((History.CallDateTime) Between CONVERT(Datetime,'20/11/2014 00:00:01',105) And CONVERT(Datetime,'20/11/2014 23:59:01',105))) 
GROUP BY Groups.GroupName, AgentTeams.TeamName, Agent.Firstname, Agent.Lastname, CRC.Description
ORDER BY Groups.GroupName, AgentTeams.TeamName;

1 个答案:

答案 0 :(得分:0)

假设您的意思是History.CallDateTime是当天:

WHERE History.CallDateTime >= CAST(CAST(GETDATE() AS DATE) AS DATETIME)
    AND History.CallDateTime < CAST(CAST(DATEADD(dd,1,GETDATE()) AS DATE) AS DATETIME) 

如果您使用的是SQL Server 2005或更早版本,则需要使用:

WHERE History.CallDateTime >= DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)
    AND History.CallDateTime < DATEADD(dd, DATEDIFF(dd, 0, getdate()) + 1, 0)