943,587 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 1239
  • MS SQL RSS
Apr 22nd, 2009
0

Parsing/Selecting DateTime fields

Expand Post »
I have a table off which I need to parse out the data using certain criteria. The table contains 3 datetime columns called 'ReadingTakenOn', 'StartedOn', and 'StoppedOn'

Here's the criteria for selecting the records:
The Dates in the 'ReadingTakenOn' column should lie between the Start Date and Stop Dates (both inclusive)

Now, I have to further pick out records from the above selected ones based on the 'Time' values as:
If the 'ReadingTakenOn' Time lies between the 'StartedOn' and 'StoppedOn'. I am not sure if I have been terribly clear on that, so here's an example:

Let's say the ReadingTakenOn column begins on 10/15/2006 08:00 and goes all the way down to 11/15/2006 18:30.

Now assume the value for 'StartedOn' is 10/26/2006 11:00 and 'StoppedOn' is 11/15/2006 16:00.

Now I want to obtain records from 10/26/2006 11:00 to 11/15/2006 16:00 and ignore records that occur before and after these dates+times.

I am struggling to write the WHERE clause for the SQL query to achieve this

Just FYI, I am using MS-SQL 2005.

Thanks in advance!
Reputation Points: 10
Solved Threads: 2
Light Poster
cheapterp is offline Offline
33 posts
since Jun 2008
Apr 23rd, 2009
0

Re: Parsing/Selecting DateTime fields

USE BETWEEN CLAUSE. LIKE: WHERE A BETWEEN C AND D .
Let me know if i cant understand your problem.
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009
Apr 23rd, 2009
0

Re: Parsing/Selecting DateTime fields

Woww... I missed an easy one there. Thanks Saion!
Reputation Points: 10
Solved Threads: 2
Light Poster
cheapterp is offline Offline
33 posts
since Jun 2008
Apr 23rd, 2009
0

Re: Parsing/Selecting DateTime fields

I have a query similar to the original poster's, so I thought I might post it here.

I want to do exactly what he/she is trying to- the only difference being that my data comes from 2 different tables. The date and time columns that correspond to the 'StartedOn' and 'StoppedOn' columns are in one table and the column corresponding to 'ReadingTakenOn' is in another table. The rest of the objective is exactly the same.

Any help is appreciated!

TIA!

P.S: I understand might have hijacked the thread here and if that is a serious flouting of the forum rules, I will be happy to start a new thread!
Last edited by Ezzaral; Apr 23rd, 2009 at 3:10 pm. Reason: Snipped "fake sig" links.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
SQL_n00b is offline Offline
23 posts
since Apr 2009
Apr 26th, 2009
0

Re: Parsing/Selecting DateTime fields

Let start is in table A and stopeed is in table b then first apply join then apply between clause. LIKE SELECT A.*,B.* FROM A INNER JOIN B ON A.ID=B.ID WHERE GETDATE() BETWEEN A.START AND B.STOP
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Problem in update query
Next Thread in MS SQL Forum Timeline: sql qustion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC