dbTalk Databases Forums  

Search Between Times

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Search Between Times in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
balaji.balaraman@gmail.com
 
Posts: n/a

Default Search Between Times - 04-21-2006 , 08:22 PM






Hi I'm new to SQl..

I have some monthly data which is being encoded as Varchar...Say for
the month of January 2005 it wud look like '200501'. For each of my
month I have a coloums which gives me datetime and volume of the job.

This is what I'm doing

Select

Month,
Time ,
Sum(Volume)

Quote:
From Table A
Where (( Month like '200501%' ) and (Time between '2005-01-01
02:00:00.000' and '2005-01-31 06:00:00.000))

group by month, time

order by month

PRoblem: What I want is all the volume between 2am and 6am on every day
of the month?

The above query naturaly will give me everything between first and last
of the month...

Any help is appreciated...

Balaji



Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Search Between Times - 04-22-2006 , 05:15 AM






(balaji.balaraman (AT) gmail (DOT) com) writes:
Quote:
Where (( Month like '200501%' ) and (Time between '2005-01-01
02:00:00.000' and '2005-01-31 06:00:00.000))

group by month, time

order by month

PRoblem: What I want is all the volume between 2am and 6am on every day
of the month?

The above query naturaly will give me everything between first and last
of the month...
convert(char(8), Time, 108) BETWEEN '02:00:00' AND '06:00:00'

You find convert under the topic CAST and CONVERT in Books Online,
where you can see the different format codes (108 in this example) that
are available.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #3  
Old   
--CELKO--
 
Posts: n/a

Default Re: Search Between Times - 04-22-2006 , 10:10 AM



Quote:
I have some monthly data which is being encoded as Varchar...Say for the month of January 2005 it would look like '200501'. For each of my month I have a columns which gives me datetime and volume of the job.
Wrong on two levels. Temporal data needs temporal data types. And if
you know the string is fixed length, why make it VARCHAR(n)? Do you
really want bad data? Did you write a proper CHECK() constaint to
prevent things like '200613'? I would bet not.

The right way is to put the range pairs into a Reporting Ranges table.

CREATE TABLE ReportRanges
(start_date DATETIME NOT NULL PRIMARY KEY,
end_date DATETIME NOT NULL,
CHECK (start_date, end_date));

INSERT INTO ReportRanges
VALUES ('2005-01-01 02:00:00.000', '2005-01-31 06:00:00.000);
etc.

Use a spreadsheet to build the data, then use a simple join. 36525
rows will cover ten years of reporting.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.