April 21, 2009 at 4:32 pm
Hello, I'm Fairly new programming with SQL and I'm stuck with this problem
I have a table with lots of records with a Date/time field.
and i need to select the records where date is between date1 and date2, but only the ones between time1 and time2
by example: Select records for the last 4 days but only the ones between 9:00am and 1:00pm.
should i have to separate the data and time in two different fields?
or there is a way to do it as it is?
Thanks ind Advance
Animo3d
April 21, 2009 at 5:35 pm
You would select all the records with a datetime greater than 4 days ago first, then filter based upon the time.
Something like:
SELECT {columns}
FROM yourTable
WHERE datecolumn >= dateadd(day, datediff(day, 0, getdate()) - 4, 0) -- 4 days ago at midnight
AND datepart(hh, datecolumn) >= 9
AND datepart(hh, datecolumn) <= 13;
Not tested - if you want a tested solution, review the article I link to in my signature on how to post questions to get better answers faster.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
April 21, 2009 at 7:41 pm
Thanks, worked very well
had a little trouble with the syntax
the correct syntax is
SELECT * FROM TableName WHERE datecolumn BETWEEN #15/04/2009# AND #22/04/2009# AND datepart('h',datecolumn )>=9 AND datepart('h',datecolumn )<=13
April 21, 2009 at 11:24 pm
I see - you are actually doing this in Access and I gave you SQL Server T-SQL syntax. At least you were able to see what was happening and convert.
Glad you were able to get it to work.
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy