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)
 Datetime, truncate time so that date be order by

Author  Topic 

dlorenc
Posting Yak Master

172 Posts

Posted - 2009-02-18 : 11:29:49
I wanted to share a solution, and ask if there might be a better one...

I wanted to truncate the time from a datetime field, so that I could order by just on the day of the datetime...with a secondary order by on the metricID field..this would show me all metrics enterd by day, ordered by metricid...

my problem was the time in the timestamp would screw up the orderby....so I wanted to strip out the time...

I used a cast/floor/cast combination...whatchathink?

SELECT KeyID, CAST(FLOOR(CAST(ReportWeek AS float)) AS datetime) AS Reportweeks, MetricID
FROM ScoreCardMetrics
ORDER BY reportweeks DESC, MetricID

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-02-18 : 11:34:42
select dateadd(day,datediff(day,0,getdate()),0) will strip out the time

Jim
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-18 : 11:42:53
quote:
Originally posted by dlorenc

I wanted to share a solution, and ask if there might be a better one...

I wanted to truncate the time from a datetime field, so that I could order by just on the day of the datetime...with a secondary order by on the metricID field..this would show me all metrics enterd by day, ordered by metricid...

my problem was the time in the timestamp would screw up the orderby....so I wanted to strip out the time...

I used a cast/floor/cast combination...whatchathink?

SELECT KeyID, CAST(FLOOR(CAST(ReportWeek AS float)) AS datetime) AS Reportweeks, MetricID
FROM ScoreCardMetrics
ORDER BY ReportWeek DESC, MetricID


modify like above and see
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2009-02-18 : 12:19:40
Jim, your solution worked also...is there any advantage in optimization to your solution to the other?..

SELECT KeyID, dateadd(day,datediff(day,0,reportweek),0) AS datetime, MetricID

FROM ScoreCardMetrics
ORDER BY datetime DESC, MetricID

visakh, no...I cannot just order by reportweek, as that field is the one that contains the time in the timestamp...so with the time included, it cannot be sorted by 'day'...

KeyID________reportweek_____________datetime____________MetricID
148____2009-02-14 08:32:23.977__2009-02-14 00:00:00.000____17
144____2009-02-14 08:32:21.803__2009-02-14 00:00:00.000____40
151____2009-02-14 08:32:25.773__2009-02-14 00:00:00.000____41

you can see in the above data..reportweek (because of the time) is useless to use in an order by...as each record is unique (it would be very rare to be able to store a record at exactly the same timestamp.)...so to order by day, I really need jim's solution to strip out the time (see the datetime field)...this then would allow me to order the metricid by day/metricid...to see that I only have one metric submitted per day....
Go to Top of Page
   

- Advertisement -