This is a candidate for a simple GROUP BY select, except that that the
INSERT_DT column in the table goes down to the second. I want to
GROUP BY at the minute level. I don't know an easy way to simply
truncate the seconds from the datetime. Extracting the time alone
won't work, because I want to compare minutes from different dates
(e.g. I am not interested in finding out if 12:47 of each day is the
highest volume minute, but rather that 23:14 of a particular day had
the highest number of inserts).
I did something ugly that works, but there has to be a better way.
Here's what I used:
cast(datename(year,INSERT_DT)+'-'+datename(month,INSERT_DT)
+'-'+datename(day,INSERT_DT)+' '+datename(hour,INSERT_DT)
+':'+datename(minute,INSERT_DT) as datetime)
It seems very strange to pull the components out of the original
datetime column, re-assemble them (sans minutes) with the stupid
dashes, spaces and colons into a string, and then re CAST them back
into a datetime.
What is the simpler way?
Thanks,
Bill
CAST(INSERT_DT AS SMALLDATETIME)
--
David Portas
These two are not equivalent. David's solution will round, mine will
truncate:
declare @d datetime
select @d = '20081212 23:00:45'
select convert(char(16), @d, 121)
select convert(smalldatetime, @d)
If you want the data to be surrected as a datetime value, change 121
to 126 in my solution.
--
Erland Sommarskog, SQL Server MVP, esq...@sommarskog.se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
(snip)
>It seems very strange to pull the components out of the original
>datetime column, re-assemble them (sans minutes) with the stupid
>dashes, spaces and colons into a string, and then re CAST them back
>into a datetime.
>
>What is the simpler way?
Hi Bill,
In addition to the methods presented by David and Erland, here's one
more:
DATEADD(minute,
DATEDIFF(minute, '20080101', INSERT_DT),
'20080101');
This method can easlliy be adapted to strip off other parts of the date.
For instance, change "minute" to "hour" (twice) to strip off minutes and
get the last whole hour.
The principle used is to calculate the number of minutes that have
passed since some base date/time (in this case: midnight, Jan. 1st 2008)
and then add that number back to the same base date/time. You can use
any base date you like, just make sure to avoid overflows (for instance,
the number of seconds since 1900 is more than the maximum integer
stores).
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
For clarity sake, I am looking for a something like this:
http://www.psoug.org/reference/builtin_functions.html only for SQL.
These things are all over the web for Oracle, but must be indexed
under diferent search terms for SQL Server.
Thanks,
Bill
Roy Harvey
Beacon Falls, CT
Cast(INSERT_DT As Smalldatetime)
"bill" <billma...@gmail.com> wrote in message
news:f762cf5a-d99a-466a...@l42g2000hsc.googlegroups.com...