473,812 Members | 2,859 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14175
select * from TableName where datepart(hh,Col umnName) 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,g etdate()),
DATEPART(quarte r,getdate()),
DATEPART(month, getdate()),
DATEPART(week,g etdate()),
DATEPART(weekda y,getdate()),
DATEPART(day,ge tdate()),
DATEPART(dayofy ear,getdate()),
DATEPART(hour,g etdate()),
DATEPART(minute ,getdate()),
DATEPART(second ,getdate()),
DATEPART(millis econd,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***@matturba nowski.co.ukwro te:
>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
issuedatetim e 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
1599
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 (appointmentStart) " & _ "SELECT myDate FROM dbo.myAppointments, c5 WHERE ... So above myDate is inserted into appointmentStart, which is a date time
2
13613
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 I insert the current date/time into the table using SQL? I used to have date/time field type in access and use the following code currTime = Now()
9
2881
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 query to display 100 records with a new, calculated field showing timelapse between time of record entry of current record and time of entry of previous record. For the first record in the dynaset, I'll settle for a value = 30 seconds. For the...
2
15154
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 the query; ERROR: date/time field value out of range: "28/05/2004 02:15:57" SQL = "insert into...
9
2431
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 doing this? --- Kyote
1
1796
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 is fine when you first enter data into the control. If you then put the cursor back into the control the data in the field expands to include seconds instead of just the date, hours and minutes. This compltely blows the input mask so that if you...
13
19986
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) format. I've been running queries by date alone, which has been working, but there are times when I have to run queries based on times as well. (e.g. return all apointments that occurred after 1:00 PM on 12/15/2009 and before 1:00 PM on 12/22/2009). ...
1
5423
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" in a query. In simple queries Nz works, but it becomes complicated after that. If you use "0" as a default value in a number or text field it stays "0". pls help
0
1540
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. the code is listed below. Any suggestions? As a work around, the import specification declares the date field as a text to maintain the values. thereafter, the data is inserted into a new table with the date field updated with the CDate...
0
9607
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10401
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10416
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10138
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9217
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.