Rounding Down To The Hour

Rounding Down To The Hour

Post by Emi Saba Blu » Fri, 19 Jan 2001 02:54:42



Hi.

What is the MOST efficient way to round a datetime field BACK to the hour.
For example
select GetDate()
might return 2001-01-17 11:42:01.163.

Now I want to return 2001-01-17 11:00:00.000.

Efficiency is the key word here.  I am using within the Query Analyzer the
following, which works.  But, when doing this to a millions of rows in a
table, the speed is poor.  What would be your suggestions.

select GetDate(),dateadd(hour,DATEPART(hour,
getdate()),convert(datetime,(Convert(varchar,GetDate(),112))))

Thanks,

blue

 
 
 

Rounding Down To The Hour

Post by Andrew J. Kell » Fri, 19 Jan 2001 03:30:19


Unless you create an index on that exact expression your performance will
always be slow since it will have to table scan. If it's something you do
ALL the time it may be worth adding an index, otherwise you will probably
have to live with the performance. The other alternative is to index the
column as  just a datetime and use a between statement with datetime values
that you generate before the select within the proper range.

--
Andrew J. Kelly
Targitmail.com



Quote:> Hi.

> What is the MOST efficient way to round a datetime field BACK to the hour.
> For example
> select GetDate()
> might return 2001-01-17 11:42:01.163.

> Now I want to return 2001-01-17 11:00:00.000.

> Efficiency is the key word here.  I am using within the Query Analyzer the
> following, which works.  But, when doing this to a millions of rows in a
> table, the speed is poor.  What would be your suggestions.

> select GetDate(),dateadd(hour,DATEPART(hour,
> getdate()),convert(datetime,(Convert(varchar,GetDate(),112))))

> Thanks,

> blue


 
 
 

Rounding Down To The Hour

Post by Nils Nilse » Fri, 19 Jan 2001 03:29:56


How about

CONVERT(smalldatetime,FLOOR(CONVERT(float,GetDate())*24)/24)
or
CONVERT(smalldatetime,CONVERT(char(14),GetDate(),120)+'00')



Quote:> Hi.

> What is the MOST efficient way to round a datetime field BACK to the hour.
> For example
> select GetDate()
> might return 2001-01-17 11:42:01.163.

> Now I want to return 2001-01-17 11:00:00.000.

> Efficiency is the key word here.  I am using within the Query Analyzer the
> following, which works.  But, when doing this to a millions of rows in a
> table, the speed is poor.  What would be your suggestions.

> select GetDate(),dateadd(hour,DATEPART(hour,
> getdate()),convert(datetime,(Convert(varchar,GetDate(),112))))

> Thanks,

> blue

 
 
 

Rounding Down To The Hour

Post by Erland Sommarsk » Tue, 23 Jan 2001 06:56:01



Quote:>What is the MOST efficient way to round a datetime field BACK to the hour.
>For example
>select GetDate()
>might return 2001-01-17 11:42:01.163.

>Now I want to return 2001-01-17 11:00:00.000.

>Efficiency is the key word here.  I am using within the Query Analyzer the
>following, which works.  But, when doing this to a millions of rows in a
>table, the speed is poor.  What would be your suggestions.

>select GetDate(),dateadd(hour,DATEPART(hour,
>getdate()),convert(datetime,(Convert(varchar,GetDate(),112))))

   SELECT convert(varchar(11), getdate(), 112)

is at least brever. As for effciency, I would like to see the
entire query.

--

 
 
 

1. Round 0.5 is rounded down instead of UP!!!

Hi there...

If I use the SQL ROUND(numeric_expression, length) function in my queries,
this happens:

    ROUND(0.4 ,1) --> 0
    ROUND(0.5 ,1) --> 0
    ROUND(0.6 ,1) --> 1

WHY!!! is 0.5 rounded down, and how can I change this!!!

Could this be a diff. between european and US standards??

Please help...

Cheers,

Palloquin

2. Rman: Duplicate database to remote host with same structure

3. Custom Dimension

4. Round hour?

5. URGENT !!!! Error with change sa password

6. dbms_job.submit to execute every round hour (12am...11pm)

7. Dynamic SQL (master/slave) scripts

8. rounding to a quater of an hour (hh:15, hh:30,hh:45,hh:00)

9. round upp to nearest 1/2 hour

10. Rounding down value

11. Average rounding down--help

12. Rounding Down GetDate()