Question

basically I have this code in sql

SELECT DISTINCT A.fDate FROM tblReport_Date A 
LEFT JOIN tblReport_Type B ON A.ID_ReportType = B.ID 
LEFT JOIN tblReceiver C ON A.ID_Receiver = C.ID 
LEFT JOIN tblClients D ON A.ID_Client = D.ID 
WHERE A.fDate BETWEEN '1/1/2013 12:00:00 AM' AND '6/1/2014 12:00:00 AM'
AND B.fType = 'HCC' AND C.Receiver = '<test@test.com>'
AND D.fClientName = 'Radford'

now, it would return

9/25/2013 8:00:00 AM
10/3/2013 12:00:00 PM
12/5/2013 10:00:00 AM
12/5/2013 12:00:00 AM 

what I wanted is something like

9/25/2013 8:00:00 AM
10/3/2013 12:00:00 PM
12/5/2013 10:00:00 AM

or even better

9/25/2013 12:00:00 AM
10/3/2013 12:00:00 AM
12/5/2013 12:00:00 AM

I know this is impossible in my current query, because

12/5/2013 10:00:00 AM and 12/5/2013 12:00:00 AM

have different TIME values. Is this possible through query?

Was it helpful?

Solution

You can group by date (without time), this should work on most versions of SQL-Server starting with 2005:

SELECT fDate = DATEADD(day, DATEDIFF(day, 0, A.fDate), 0) 
FROM tblReport_Date A 
LEFT JOIN tblReport_Type B ON A.ID_ReportType = B.ID 
LEFT JOIN tblReceiver C ON A.ID_Receiver = C.ID 
LEFT JOIN tblClients D ON A.ID_Client = D.ID 
WHERE A.fDate BETWEEN '1/1/2013 12:00:00 AM' AND '6/1/2014 12:00:00 AM'
AND B.fType = 'HCC' AND C.Receiver = '<test@test.com>'
AND D.fClientName = 'Radford'
GROUP BY DATEADD(day, DATEDIFF(day, 0, A.fDate), 0)

Or in SQL Server 2008 onwards you cast to Date:

GROUP BY CAST(A.fDate AS DATE)

OTHER TIPS

;WITH CTE_Temp ( SELECT DISTINCT A.fDate FROM tblReport_Date A LEFT JOIN tblReport_Type B ON A.ID_ReportType = B.ID LEFT JOIN tblReceiver C ON A.ID_Receiver = C.ID LEFT JOIN tblClients D ON A.ID_Client = D.ID WHERE A.fDate BETWEEN '1/1/2013 12:00:00 AM' AND '6/1/2014 12:00:00 AM' AND B.fType = 'HCC' AND C.Receiver = '<test@test.com>' AND D.fClientName = 'Radford' ) SELECT DISTINCT DATE(fDate) FROM CTE_Temp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top