473,386 Members | 1,962 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,386 software developers and data experts.

Filtering on parts of a date/time field

Hi,
I have a date/time field in a SQL2000 database, and what I would like
to do is to filter on a specific part of the field, for example the
time or hour.

Supposing I have a set of data for the last 5 years and would like to
filter out any records which are outside working hours i.e. I would
like to show records where the time is between 9am and 5pm.

Does anyone know if there is a simple way to do this in SQL? If you
simply don't specify the date part in the where clause (e.g. WHERE
issuedatetime between '09:00:00' and '17:00:00') it defaults it to
1900-01-01 so basically no data is returned.
The only way I can see to do this is by using the DATEPART function,
converting it to a varchar, appending 1900-01-01 on to it and
converting it to a datetime, and then using the where clause as stated
above. This is quite a long-winded way, however. Any other suggestions?

Thanks,
Matt

Jan 3 '07 #1
3 14157
select * from TableName where datepart(hh,ColumnName) between 9 and 17

Denis the SQL Menace
http://sqlservercode.blogspot.com/

matturbanowski wrote:
Hi,
I have a date/time field in a SQL2000 database, and what I would like
to do is to filter on a specific part of the field, for example the
time or hour.

Supposing I have a set of data for the last 5 years and would like to
filter out any records which are outside working hours i.e. I would
like to show records where the time is between 9am and 5pm.

Does anyone know if there is a simple way to do this in SQL? If you
simply don't specify the date part in the where clause (e.g. WHERE
issuedatetime between '09:00:00' and '17:00:00') it defaults it to
1900-01-01 so basically no data is returned.
The only way I can see to do this is by using the DATEPART function,
converting it to a varchar, appending 1900-01-01 on to it and
converting it to a datetime, and then using the where clause as stated
above. This is quite a long-winded way, however. Any other suggestions?

Thanks,
Matt
Jan 3 '07 #2
Look at the datepart function:

SELECT DATEPART(year,getdate()),
DATEPART(quarter,getdate()),
DATEPART(month,getdate()),
DATEPART(week,getdate()),
DATEPART(weekday,getdate()),
DATEPART(day,getdate()),
DATEPART(dayofyear,getdate()),
DATEPART(hour,getdate()),
DATEPART(minute,getdate()),
DATEPART(second,getdate()),
DATEPART(millisecond,getdate())

It is also worth becoming familiar with DATEADD() and DATENAME().

Roy Harvey
Beacon Falls, CT

On 3 Jan 2007 10:02:48 -0800, "matturbanowski"
<sa***@matturbanowski.co.ukwrote:
>Hi,
I have a date/time field in a SQL2000 database, and what I would like
to do is to filter on a specific part of the field, for example the
time or hour.

Supposing I have a set of data for the last 5 years and would like to
filter out any records which are outside working hours i.e. I would
like to show records where the time is between 9am and 5pm.

Does anyone know if there is a simple way to do this in SQL? If you
simply don't specify the date part in the where clause (e.g. WHERE
issuedatetime between '09:00:00' and '17:00:00') it defaults it to
1900-01-01 so basically no data is returned.
The only way I can see to do this is by using the DATEPART function,
converting it to a varchar, appending 1900-01-01 on to it and
converting it to a datetime, and then using the where clause as stated
above. This is quite a long-winded way, however. Any other suggestions?

Thanks,
Matt
Jan 3 '07 #3
OK, Thanks for your replies.

Jan 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gil | last post by:
I'm trying to INSERT based on info selected from another table. How can I concatenate a string of time to the column 'myDate' below (the value being inserted)? INSERT INTO c5...
2
by: JP SIngh | last post by:
Can someone please suggest the correct way to store the date & time in SQL Server? I want to store the date and time a record was created. What should the field type be in SQL Server? How can...
9
by: MLH | last post by:
I have a database (datatrek.mdb) with a table named DATA. The table has a date/time field with default value = Now(). It has 100 records in it entered over a 50-minute period. I would like the...
2
by: kaka | last post by:
I'm runnig Redhat 7.3 with postgresql 7.3.4 builded from sources and upgraded recently. I receive this error ( never received before upgrade ) and I don' t know what to do. Error while executing...
9
by: Kyote | last post by:
I have a textbox that is databound to a table in an access database. The field only contains month/day/year but my databound textbox is also showing a time. Is there any way to prevent it from...
1
by: BoxTop | last post by:
Here is an Access 97 problem that's been bugging me for years. On a form I set up a date/time field. I set up the format to be dd/MM/yy hh:nn. I set the input mask to be ##/##/##\ ##:##;0;_ All...
13
by: dizzydangler | last post by:
Just a quick question...I'm running an MS Access 2007 db that tracks appointments in a single table. Date and time are entered as separate fields in short date (mm/dd/yyyy) and short time (hh:mm)...
1
by: neelsfer | last post by:
Is there a way of making a Date/time field in a table a "null value"? I would like to have "long time" as a null value. 05/12/2010 12:38:31 PM. Reason - to add up all the fields with "null values"...
0
by: dowlingm815 | last post by:
I currently importing a csv file with an import specification declaring the field values. one field is a date/time field. when the csv file is imported, it clears the date field with null values....
0
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,...
0
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$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.