Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 group by date but not date time

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2008-02-28 : 04:21:02
i have a query
select mydate,lastname from users
group by mydate,lastname

now the only thing is mydate is a datetime fields but i want the results to group by just the date for each lastname entered.
How do i do this?

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-28 : 04:23:22
select dateadd(day, datediff(day, '19000101', mydate), '19000101') AS theDay ,lastname from users
group by dateadd(day, datediff(day, '19000101', mydate), '19000101'),lastname



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-28 : 04:31:53
or

select dateadd(day, datediff(day, 0, mydate), 0) AS mydate ,lastname from users
group by dateadd(day, datediff(day, 0, mydate), 0),lastname


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-28 : 04:54:16
or

select dateadd(day, datediff(day, '99991231', mydate), '99991231') AS theDay ,lastname from users
group by dateadd(day, datediff(day, '99991231', mydate), '99991231'),lastname

I just use '19000101' for clarification since BOL states the 2nd and 3rd parameter to be dates.
Not many beginners will notice that 0 is the same as '19000101' because of internal conversion.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -