473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Query not returning record when searching range of dates

106 New Member
Hi,
in my SQl database, I have a WHERE statement to search a record based on two dates.
using the code below, Query will not return a record if SearchDate=FromDate.

Amongst my records, an example date range in database is:
Expand|Select|Wrap|Line Numbers
  1. FromDate = 2014-04-10 17:32:55.000
  2. ToDate=    2014-04-16 17:32:55.000
Search fails for 10/04/14.
Other dates (11/04/14, 12/04/14 etc) will be returned.

SQL Code
Expand|Select|Wrap|Line Numbers
  1. WHERE PHD.FromDate <= Convert(DateTime, '10/04/2014' ,103)      
  2. AND PHD.ToDate >= Convert(DateTime, '11/04/2014' ,103)
  3.  
VB2010 Code
Expand|Select|Wrap|Line Numbers
  1. " WHERE PHD.FromDate <= convert(DateTime, '" & txtPCNDate.Value.ToString("dd/MM/yyyy") & "' ,103)   " & _
  2. "   AND PHD.ToDate >= convert(DateTime, '" & txtPCNDate.Value.AddDays(1).ToString("dd/MM/yyyy") & "' ,103)      " & _
What am I missing from the query?
May 30 '14 #1

✓ answered by Luuk

Your last step is wrong.
Yes, it does produce the desired output, true!

Because you are using a function on a databasecolumn, the database will no longer use the index that you probable have defined on that field.

So i suggest rewrinting this:
Expand|Select|Wrap|Line Numbers
  1.  
  2.     WHERE (PHD.FromDate <= CONVERT(Date, '17/04/2014' , 103) 
  3.     AND PHD.ToDate>=CONVERT(Date, '17/04/2014' , 103))
  4.     OR 
  5.     (Convert(Date, PHD.FromDate) = CONVERT(Date, '17/04/2014' , 103))
  6.  
to this:
Expand|Select|Wrap|Line Numbers
  1. WHERE (PHD.FromDate <= CONVERT(DateTime, '17/04/2014 23:59:59' , 103) 
  2.     AND PHD.ToDate>=CONVERT(DateTime, '17/04/2014 00:00:00' , 103))
  3.  
Because the fields 'FromDate' and 'ToDate' are datetime values, you better compare them to datetime values.

P.S. Maybe i got the time wrong in this last query, but that's because i still dont understand what you are actually trying to select.... ;)

11 2301
vmm1234
1 New Member
From the code you are comparing the dates in Datetime format.

So

PHD.FromDate <= Convert(DateTime, '10/04/2014' ,103)

becomes

'2014-04-10 17:32:55.000' <= '2014-04-10 00:00:00.000'

You may want to change your code to

Expand|Select|Wrap|Line Numbers
  1. WHERE DATE(PHD.FromDate) <= Convert(DateTime, '10/04/2014' ,103)       
  2. AND DATE(PHD.ToDate) >= Convert(DateTime, '11/04/2014' ,103) 
  3.  
May 30 '14 #2
tasawer
106 New Member
Thanks for the reply.
Your code not work for me. However, I changed it to
Expand|Select|Wrap|Line Numbers
  1. Convert(DATE,PHD.FromDate) >= Convert(DateTime, '09/04/2014' ,103)       
  2. AND Convert(DATE,PHD.ToDate) <= Convert(DateTime, '10/04/2014' ,103)
  3.  
Where it found the example date, but other dates went missing.

Below is my full range of dates for one record. I need to be able to search for any date.
Please advise on the how the code can be changed and the logic.

Expand|Select|Wrap|Line Numbers
  1.  ID    FromDate            ToDate
  2. 9132    2014-02-13 17:32:55.000    2014-02-19 17:32:55.000
  3. 9133    2014-02-20 17:32:55.000    2014-02-26 17:32:55.000
  4. 9134    2014-02-27 17:32:55.000    2014-03-05 17:32:55.000
  5. 9135    2014-03-06 17:32:55.000    2014-03-12 17:32:55.000
  6. 9136    2014-03-13 17:32:55.000    2014-03-19 17:32:55.000
  7. 9137    2014-03-20 17:32:55.000    2014-03-26 17:32:55.000
  8. 9138    2014-03-27 17:32:55.000    2014-04-02 17:32:55.000
  9. 9139    2014-04-03 17:32:55.000    2014-04-09 17:32:55.000
  10. 9140    2014-04-10 17:32:55.000    2014-04-16 17:32:55.000
  11. 9141    2014-04-17 17:32:55.000    2014-04-23 17:32:55.000
  12. 9142    2014-04-24 17:32:55.000    2014-04-30 17:32:55.000
  13.  
May 30 '14 #3
Luuk
1,047 Recognized Expert Top Contributor
you have the '<=' and '>=' wrong...

Expand|Select|Wrap|Line Numbers
  1. select current_timestamp 
  2. where 
  3.   current_timestamp between convert(DateTime, current_timestamp,103) and convert(DateTime,current_timestamp+1,103);
  4.  
or

Expand|Select|Wrap|Line Numbers
  1. select current_timestamp 
  2. where current_timestamp >= convert(DateTime, current_timestamp,103) 
  3.   and current_timestamp <= convert(DateTime,current_timestamp+1,103);
  4.  
May 30 '14 #4
tasawer
106 New Member
These suggestions also fail.
currently we are checking for both dates in one query.
My instincts tell me that I need to run a query on FROMDATE first and then Requery the TODATE
So I would need a Select statement within the where. but not sure how.
May 30 '14 #5
Rabbit
12,516 Recognized Expert Moderator MVP
If you are looking for whether or not the time periods overlap at all, then you need to look for where the start of period A is before the end of period B and the end of period A is after the start of period B.

However, if you are looking for if one time period is fully encompassed by another time period, then you need to check that both the start and end of period A is between the start and end of period B.

And also, if you are looking at just dates, then you need to convert everything to dates and not datetime.
May 30 '14 #6
Luuk
1,047 Recognized Expert Top Contributor
I'm sorry, but i dont understand your question,
or do you mean to return all the info except for the records between two given dates?

Then you need:
Expand|Select|Wrap|Line Numbers
  1. WHERE PHD.FromDate <= Convert(DateTime, '10/04/2014' ,103)      
  2.     OR PHD.ToDate >= Convert(DateTime, '11/04/2014' ,103)
  3.  
or

Expand|Select|Wrap|Line Numbers
  1. WHERE NOT (PHD.FromDate BETWEEN Convert(DateTime, '10/04/2014' ,103)      
  2.     AND PHD.ToDate >= Convert(DateTime, '11/04/2014' ,103))
  3.  
May 30 '14 #7
tasawer
106 New Member
Thanks for your suggestions.
I finally resolved it with the code below.
It works a treat.

Expand|Select|Wrap|Line Numbers
  1. WHERE (PHD.FromDate <= CONVERT(Date, '17/04/2014' , 103) 
  2. AND PHD.ToDate>=CONVERT(Date, '17/04/2014' , 103))
  3. OR 
  4. (Convert(Date, PHD.FromDate) = CONVERT(Date, '17/04/2014' , 103))
  5.  
May 31 '14 #8
Luuk
1,047 Recognized Expert Top Contributor
Somehow i'm under the impression that you still do not know WHY/HOW you solved this.....

Am i correct?
May 31 '14 #9
tasawer
106 New Member
I am absolutely certain on how I resolved it.
it was tough though(for me).

I went through a range of eight sequential dates and monitored the output and changed the code accordingly.

As time is also stored with the date, it was causing some of the dates not to be returned.
I resolved that by converting datetime to date only and then comparing.
Expand|Select|Wrap|Line Numbers
  1. OR 
  2. (Convert(Date, PHD.FromDate) = CONVERT(Date, '17/04/2014' , 103))
  3.  
of course, if there is an improved solution, I'm all ears to it.
May 31 '14 #10
Luuk
1,047 Recognized Expert Top Contributor
Your last step is wrong.
Yes, it does produce the desired output, true!

Because you are using a function on a databasecolumn, the database will no longer use the index that you probable have defined on that field.

So i suggest rewrinting this:
Expand|Select|Wrap|Line Numbers
  1.  
  2.     WHERE (PHD.FromDate <= CONVERT(Date, '17/04/2014' , 103) 
  3.     AND PHD.ToDate>=CONVERT(Date, '17/04/2014' , 103))
  4.     OR 
  5.     (Convert(Date, PHD.FromDate) = CONVERT(Date, '17/04/2014' , 103))
  6.  
to this:
Expand|Select|Wrap|Line Numbers
  1. WHERE (PHD.FromDate <= CONVERT(DateTime, '17/04/2014 23:59:59' , 103) 
  2.     AND PHD.ToDate>=CONVERT(DateTime, '17/04/2014 00:00:00' , 103))
  3.  
Because the fields 'FromDate' and 'ToDate' are datetime values, you better compare them to datetime values.

P.S. Maybe i got the time wrong in this last query, but that's because i still dont understand what you are actually trying to select.... ;)
May 31 '14 #11
tasawer
106 New Member
Perfect... after my own solution, I thought I was the king :)
May 31 '14 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: M.Stanley | last post by:
Hi, I have a problem..I'm doing a specific query where I'm joining fields from a table with appednded data (there are duplicate records, except for the date/time), and another query. I want the...
6
by: Derrick | last post by:
Hello all; Since I do have working code, this is more for my curiosity only. I'm creating a "Plugin" architecture, following some of the many examples on the 'net. Basically what I have is...
3
by: Sim Zacks | last post by:
I am using 8.0 beta 1 on an RH 8 Linux server. I have a union query that I am converting from access (where it worked) and it is returning duplicates. The only difference between the two rows is...
1
by: accessbee | last post by:
(Sorry this is so long, was trying to fully explain the situation) There are two tables that handle information on our clients. Every client has a unique ClientID, and the ClientID is the Primary Key...
10
by: Phil Latio | last post by:
How do I use wildcards when searching in array? At least that's what I think I need !! I have the line: if ($attribute != "id") The above is not 100% correct because it should also be...
6
by: PsyClone | last post by:
Im fairly new to access and would prefer not to use any scripting as such, but Im trying to create aselect query based on two tables: tblProduction, from which the query uses DeptID, ProductionQty, ...
1
by: ruvi | last post by:
Hi, I am using Vb 6 and Access. I am getting syntax error in the following sql query. strSql = "SELECT ...
1
matthardwick
by: matthardwick | last post by:
I have a form that has lots of sub forms on it. The user shouldn't be editing the information that isn't part of the sub forms, and the fields are locked... but when a user presses return/enter -...
1
by: bkberg05 | last post by:
Hi -I have a form which contains a sub-form. The sub-form is tied to the main form by a shared field called Project_ID. The main form has one record per Project. The sub-form has many records per...
4
by: NarenKeer | last post by:
Hi, I have been trying to find a solution for this from past one week and no good progress yet. I just want to avoid the parameter window displayed by query in record source property of a...
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...
1
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.