473,404 Members | 2,137 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,404 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 14158
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: 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
1
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...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.