473,380 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,380 software developers and data experts.

Selecting From Multiple Time Periods

I greatly appreciate your help as I have been trying to come up with the correct sql statement but output is inaccurate. How do you write the correct t-sql statement for the following:

SELECT FieldValue WHERE DateTime is from 9:30PM - 8:30AM on Mondays thru Fridays, AND ALL-DAY (i.e. all DateTime values) on Saturdays & Sundays for the whole month of January 2008.

I am using SQL Server 2000. Thank you.
Apr 11 '08 #1
2 1858
1,134 Expert 1GB
Hi Descarre and welcome

First, here are a few facts that we can use to achieve a solution
1) DatePart is a function that can be used to find out all sorts of info about a datetime value
2) The WeekDay number for saturday and sunday are 7 and 1 respectively
so we can use that to return all of the records for those days.
3) The Hour part + (the minute part/100) will give us a decimal number
we can use that to return the data between the times you want
4) The Month function returns the month number
we can use that to return the data for a particular month

Using those facts, here is one way of achieving what you want
Expand|Select|Wrap|Line Numbers
  1. select DateTime,FieldValue
  2. FROM
  3. (   select DateTime,
  4.          (datepart(hh,datetime)*1.0)+(datepart(mi,DateTime)/100.0) as T,
  5.          FieldValue,
  6.          datepart(dw,DateTime)as d 
  7.     from YourTable
  8. )a
  9. WHERE (d=1 or d=7 or T>=21.3 or T<=8.3) and Month(DateTime)=1

Oh, by the way, I don't think its a good idea to name a field the same as a data type. It might be a little confusing if you need to debug your query in 6 months time.
Apr 11 '08 #2
2,878 Expert 2GB
I think the weekday number may be affected by setting the first day of the week server setting. You may also use the datename function and just check the string names of the day. Something like:

Expand|Select|Wrap|Line Numbers
  1. where  datename(dw,YourDateField) in ('Saturday', 'Sunday') 

-- CK
Apr 12 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Manu | last post by:
Hi, Here's what i want to accomplish. I want to make a list of frequenctly occuring words in a group of files along with the no of occurances of each The brute force method will be to read the...
by: james.dixon | last post by:
Hi I was wondering if anyone else had had this problem before (can't find anything on the web about it). I have three select elements (list boxes - from here on I'll refer to them as 'the...
by: Steve Jorgensen | last post by:
I've run across this issue several times of late, and I've never come up with a satisfactory answer to the best way to handle this schema issue. You have a large section of schema in which a...
by: Alphonse Giambrone | last post by:
Is there a way to use multiple search patterns when calling Directory.GetFiles. For instance Directory.GetFiles("C:\MyFolder", "*.aspx") will return all files with the aspx extension. But what if...
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been...
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
by: TC | last post by:
I need to design a system which represents multiple "projects" in SQL Server. Each project has the same data model, but is independent of all others. My inclination is to use one database to store...
by: Smithers | last post by:
Until now I have worked on small teams (1-3 developers) and we've been able to stay out of each others way. Now I'm about to start work on a project that will have 5 developers. I would appreciate...
by: D. Yates | last post by:
Hi, I'm about to embark on a project that will both send and receive information to/from our client computers in the field. The area that I still need to finalize is the method of...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.